匹配后如何添加特定键值?

时间:2016-06-10 06:51:21

标签: php arrays foreach

我有一个数组:

  Array
(
    [0] => Array
        (
            [cam_id] => 136
            [partner_id] => 350
            [marketing] => [{"quartername":"q1","tactic":"Linkedin(Connections & Pages)","1":"9000","2":"4000","3":"3000","4":"2000","5":"1400","6":"1000","7":"700","8":"600","9":"500","10":"65"},{"quartername":"q1","tactic":"Single Email Campaign","1":"350","2":"200","3":"156","4":"142","5":"122","6":"112","7":"80","8":"65","9":"35","10":"15"},{"quartername":"q4","tactic":"Single Email Campaign","1":"350","2":"200","3":"156","4":"142","5":"122","6":"112","7":"80","8":"65","9":"35","10":"15"}]
        )

)
在解读了#34;营销"的杰森之后关键值我得到了这个数组。

Array
(
    [0] => Array
        (
            [quartername] => q1
            [tactic] => Linkedin(Connections & Pages)
            [1] => 9000
            [2] => 4000
            [3] => 3000
            [4] => 2000
            [5] => 1400
            [6] => 1000
            [7] => 700
            [8] => 600
            [9] => 500
            [10] => 65
        )

    [1] => Array
        (
            [quartername] => q1
            [tactic] => Single Email Campaign
            [1] => 350
            [2] => 200
            [3] => 156
            [4] => 142
            [5] => 122
            [6] => 112
            [7] => 80
            [8] => 65
            [9] => 35
            [10] => 15
        )

    [2] => Array
        (
            [quartername] => q4
            [tactic] => Single Email Campaign
            [1] => 350
            [2] => 200
            [3] => 156
            [4] => 142
            [5] => 122
            [6] => 112
            [7] => 80
            [8] => 65
            [9] => 35
            [10] => 15
        )

)

我想查看"季度名称"键如果数组中的键匹配则相应地添加。

我希望结果数组就像添加相同的" quartername"键:

    Array
(
    [0] => Array
        (
            [quartername] => q1
            [tactic] => 
            [1] => 9350
            [2] => 4200
            [3] => 3156
            [4] => 2142
            [5] => 1522
            [6] => 1112
            [7] => 780
            [8] => 665
            [9] => 535
            [10] => 80
        )


    [1] => Array
        (
            [quartername] => q4
            [tactic] => Single Email Campaign
            [1] => 350
            [2] => 200
            [3] => 156
            [4] => 142
            [5] => 122
            [6] => 112
            [7] => 80
            [8] => 65
            [9] => 35
            [10] => 15
        )

)

这是我试过的代码。它将添加" quartername"的所有值。键。

foreach($actual_leads as $key => $val){
                $actual_leads[$key]['marketing'] = json_decode($actual_leads[$key]['marketing'],true);
                echo "<pre>"; print_r($actual_leads[$key]['marketing']);echo "</pre>";
            } 

            $actual_leads_tot = array();
            foreach($actual_leads as $key => $val){

                foreach($val as $key2 => $val2){

                    foreach($val2 as $key3=> $val3){

                        foreach($val3 as $key4=>$val4){

                            if($key4 != 'quartername' && $key4 != 'tactic'){
                                $actual_leads_tot[$key][$key4] += $val4;
                                $actual_leads[$key]['marketing'] = $actual_leads_tot;
                            }
                        }
                    }
                }
            } 

2 个答案:

答案 0 :(得分:1)

只需使用quatername作为密钥:

<?php
  $actual_leads_tot = array();

  foreach($actual_leads as $lead) {
    foreach($lead as $key => $item) {
      if(isset($actual_leads_tot[$lead['quatername']][$key] && $key != 'quatername' && $key != 'tactic') {
        $item = $item + $actual_leads_tot[$lead['quatername']][$key];
      }

      $actual_leads_tot[$lead['quatername']][$key] = $item;
    }
  }
?>

现在你有一个完整的组合线索阵列。

答案 1 :(得分:1)

$actual_leads3 =  array();
        foreach($actual_leads2 as $key=> $val ){
            foreach($val as $key1 => $val1){
                if(in_array($key1,$_POST['sel_qtrs'])){
                    $actual_leads3[$key][$key1] = $actual_leads2[$key][$key1]; // Filtered array
                }
            }
        }

        $actual_total3 =  array();
        foreach($actual_leads3 as $key=>$val){
            foreach($val as $key1=>$val1){
                foreach($val1 as $key2=>$val2){
                    foreach($val2 as $key3=>$val3){
                        //$actual_leads3[$key][$key1]['total'][$key3] += $val3;
                        $actual_total3[$key][$key1][$key3] += $val3;
                        //echo "<pre>"; print_r($key3); echo "</pre>";

                    }
                }
            }
        }