在一个数组索引中组合和添加数据?

时间:2016-06-16 10:29:57

标签: php arrays foreach

我有一个数组:

Array
(
    [0] => Array
        (
            [spid] => 20
            [name] => Sunny
            [clientid] => 15
            [selectedproduct] => ["190","191"]
            [memberid] => 637
            [partner_id] => 390
            [monthly_forecast] => a:2:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";}

            [growth_plan] => {"product":["190","191"],"plans":["8","8"]}
            [other_palns] => {"product":["190","191"],"plans":["8","8"],"total":["4000","5000"]}
            [marketing_s_values] => {"190":"2400","191":"7200"}
            [marketing_s_values_monthly] => {"190":[{"1":"150","2":"150","3":"150","4":"150","5":"150","6":"150","7":"150","8":"150","9":"150","10":"150","11":"150","12":"150"},{"1":"200","2":"200","3":"200","4":"200","5":"200","6":"200","7":"200","8":"200","9":"200","10":"200","11":"200","12":"200"},{"1":"250","2":"250","3":"250","4":"250","5":"250","6":"250","7":"250","8":"250","9":"250","10":"250","11":"250","12":"250"},{"1":"300","2":"300","3":"300","4":"300","5":"300","6":"300","7":"300","8":"300","9":"300","10":"300","11":"300","12":"300"},{"1":"350","2":"350","3":"350","4":"350","5":"350","6":"350","7":"350","8":"350","9":"350","10":"350","11":"350","12":"350"}],"191":[{"1":"500","2":"500","3":"500","4":"500","5":"500","6":"500","7":"500","8":"500","9":"500","10":"500","11":"500","12":"500"},{"1":"600","2":"600","3":"600","4":"600","5":"600","6":"600","7":"600","8":"600","9":"600","10":"600","11":"600","12":"600"},{"1":"700","2":"700","3":"700","4":"700","5":"700","6":"700","7":"700","8":"700","9":"700","10":"700","11":"700","12":"700"},{"1":"800","2":"800","3":"800","4":"800","5":"800","6":"800","7":"800","8":"800","9":"800","10":"800","11":"800","12":"800"},{"1":"900","2":"900","3":"900","4":"900","5":"900","6":"900","7":"900","8":"900","9":"900","10":"900","11":"900","12":"900"}]}

        )

    [1] => Array
        (
            [spid] => 20
            [name] => Sunny
            [clientid] => 15
            [selectedproduct] => ["199","210"]
            [memberid] => 641
            [partner_id] => 394
            [monthly_forecast] => a:2:{i:0;s:163:"[["10","0","0","0","30","0","0","60","0","0","0","0"],["20","0","0","30","0","0","20","0","0","0","50","0"],["30","0","0","0","20","0","0","30","0","20","0","30"]]";i:1;s:154:"[["20","0","0","0","0","0","0","0","0","0","0","0"],["30","0","0","0","0","0","0","0","0","0","0","0"],["40","0","0","0","0","0","0","0","0","0","0","0"]]";}

            [growth_plan] => {"product":["199","210"],"plans":["8","8"]}

            [other_palns] => {"product":["199","210"],"plans":["10","10"],"total":["6000","3000"]}
            [marketing_s_values] => {"199":"4700","210":"0"}
            [marketing_s_values_monthly] => {"199":[{"1":"4600","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"4700","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"4800","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"4900","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"5000","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"}],"210":[{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"},{"1":"0","2":"0","3":"0","4":"0","5":"0","6":"0","7":"0","8":"0","9":"0","10":"0","11":"0","12":"0"}]}

        )

    [2] => Array
        (
            [spid] => 20
            [name] => Sunny

            [clientid] => 15
            [selectedproduct] => ["190","191","192","193"]
            [memberid] => 574
            [partner_id] => 374

            [monthly_forecast] => a:4:{i:0;s:151:"[["1","0","0","1","0","0","1","0","1","0","0","0"],["1","0","0","0","1","0","0","0","1","0","1","1"],["1","0","1","0","1","0","1","0","1","0","0","1"]]";i:1;s:151:"[["1","0","1","0","1","0","1","0","1","0","1","0"],["1","0","1","0","1","1","0","1","0","1","0","1"],["1","0","1","0","1","0","1","1","1","0","1","1"]]";i:2;s:151:"[["1","1","1","0","1","1","1","0","0","0","1","1"],["1","1","1","0","0","1","1","1","1","0","1","1"],["1","1","1","1","0","0","1","1","1","1","1","1"]]";i:3;s:151:"[["1","0","1","0","0","0","1","0","1","0","0","2"],["1","0","0","2","1","0","1","0","1","1","0","1"],["1","0","2","1","1","1","0","1","0","1","1","1"]]";}

            [growth_plan] => {"product":["190","191","192","193"],"plans":["8","8","9","8"]}

            [other_palns] => {"product":["190","191","192","193"],"plans":["8","8","8","8"],"total":["4000","5000","1500","36000"]}
            [marketing_s_values] => {"190":"2400","191":"7200","192":"2400","193":"24000"}

            [marketing_s_values_monthly] => {"190":[{"1":"150","2":"150","3":"150","4":"150","5":"150","6":"150","7":"150","8":"150","9":"150","10":"150","11":"150","12":"150"},{"1":"200","2":"200","3":"200","4":"200","5":"200","6":"200","7":"200","8":"200","9":"200","10":"200","11":"200","12":"200"},{"1":"250","2":"250","3":"250","4":"250","5":"250","6":"250","7":"250","8":"250","9":"250","10":"250","11":"250","12":"250"},{"1":"300","2":"300","3":"300","4":"300","5":"300","6":"300","7":"300","8":"300","9":"300","10":"300","11":"300","12":"300"},{"1":"350","2":"350","3":"350","4":"350","5":"350","6":"350","7":"350","8":"350","9":"350","10":"350","11":"350","12":"350"}],"191":[{"1":"500","2":"500","3":"500","4":"500","5":"500","6":"500","7":"500","8":"500","9":"500","10":"500","11":"500","12":"500"},{"1":"600","2":"600","3":"600","4":"600","5":"600","6":"600","7":"600","8":"600","9":"600","10":"600","11":"600","12":"600"},{"1":"700","2":"700","3":"700","4":"700","5":"700","6":"700","7":"700","8":"700","9":"700","10":"700","11":"700","12":"700"},{"1":"800","2":"800","3":"800","4":"800","5":"800","6":"800","7":"800","8":"800","9":"800","10":"800","11":"800","12":"800"},{"1":"900","2":"900","3":"900","4":"900","5":"900","6":"900","7":"900","8":"900","9":"900","10":"900","11":"900","12":"900"}],"192":[{"1":"100","2":"100","3":"100","4":"100","5":"100","6":"100","7":"100","8":"100","9":"100","10":"100","11":"100","12":"100"},{"1":"200","2":"200","3":"200","4":"200","5":"200","6":"200","7":"200","8":"200","9":"200","10":"200","11":"200","12":"200"},{"1":"300","2":"300","3":"300","4":"300","5":"300","6":"300","7":"300","8":"300","9":"300","10":"300","11":"300","12":"300"},{"1":"400","2":"400","3":"400","4":"400","5":"400","6":"400","7":"400","8":"400","9":"400","10":"400","11":"400","12":"400"},{"1":"500","2":"500","3":"500","4":"500","5":"500","6":"500","7":"500","8":"500","9":"500","10":"500","11":"500","12":"500"}],"193":[{"1":"1500","2":"1500","3":"1500","4":"1500","5":"1500","6":"1500","7":"1500","8":"1500","9":"1500","10":"1500","11":"1500","12":"1500"},{"1":"2000","2":"2000","3":"2000","4":"2000","5":"2000","6":"2000","7":"2000","8":"2000","9":"2000","10":"2000","11":"2000","12":"2000"},{"1":"2200","2":"2200","3":"2200","4":"2200","5":"2200","6":"2200","7":"2200","8":"2200","9":"2200","10":"2200","11":"2200","12":"2200"},{"1":"2500","2":"2500","3":"2500","4":"2500","5":"2500","6":"2500","7":"2500","8":"2500","9":"2500","10":"2500","11":"2500","12":"2500"},{"1":"2700","2":"2700","3":"2700","4":"2700","5":"2700","6":"2700","7":"2700","8":"2700","9":"2700","10":"2700","11":"2700","12":"2700"}]}

        )

在这个数组中你看到" spid"类似于所有数组index.i只是希望它将所有数据合并到一个索引数组中。

如您所见" selectedproduct"索引0和索引2的类似数据类似于190和191,只要"选择产品"是相似的我只是想在那里各自" monthly_forecast"值彼此相加并将所有三个索引值合并为一个,但仅限于" selectedproduct"类似于那些" monthly_forecast"增值。

我试过这段代码:

$new_values = array();
                                            foreach($get_partner_same_forecast as $value) {
                                                if(isset($new_values[$value['spid']])) {
                                                    $temp = $new_values[$value['spid']];
                                                    $temp['selectedproduct'] .= ',' . $value['selectedproduct'];
                                                    $user[] = json_decode($value['selectedproduct'],true);
                                                    $result = array();
                                                    foreach($user as $item) {
                                                        $result = array_merge($result, $item);  
                                                    }
                                                    $temp['selectedproduct']=json_encode($result);
                                                    $temp['partner_acc_name'] .= ',' . $value['partner_acc_name'];//mergepartner name       
                                                    $temp['crm_acc_name'] .= ',' . $value['crm_acc_name']; //merge crmname
                                                    $temp['memberid'] .= ',' . $value['memberid'];//merge memberid
                                                    $temp['partner_id'] .= ',' . $value['partner_id'];//merge partnerid
                                                    $temp['growth_plan'] .= ',' . $value['growth_plan'];//merge growth plan
                                                    $user1[] = json_decode($value['growth_plan'],true);
                                                    $product = array();
                                                    $plan = array();
                                                    foreach($user1 as $k=>$v)
                                                    {
                                                        $product = array_merge($product, $v['product']);    
                                                        $plan = array_merge($plan, $v['plans']);    
                                                        $user1[$k]['product'] = $product;
                                                        $user1[$k]['plans'] = $plan;
                                                    }
                                                    $temp['growth_plan']=json_encode($user1[1]);

                                                    $temp['monthly_forecast'] .= ',' . $value['monthly_forecast'];//merge unserialize monthly data
                                                    $data1=array();
                                                    $data2=array();
                                                    $data2=unserialize($value['monthly_forecast']);
                                                    $data1=unserialize($temp['monthly_forecast']);
                                                    $combinedData=array();
                                                    $combinedData = array($data1, $data2);  
                                                    $monthly_forecast = array();
                                                    $monthly_forecast=serialize($combinedData);

                                                    $temp['monthly_forecast']=$monthly_forecast;//merge monthly 
                                                    $monthly_forecast=unserialize($monthly_forecast);

                                                    //echo "<pre>"; print_r($monthly_forecast);echo "</pre>";

                                                    $new_values[$value['spid']] = $temp;
                                                    } else {
                                                    $new_values[$value['spid']] = $value;
                                                }
                                            }
                                            $new_values = array_values($new_values);

0 个答案:

没有答案