第一个数组
array(3) {
 [0] => array(3) {
   ["cxcs"] => int(77)
   ["juxiang"] => string(9) "北京"
   ["jieguo"] => string(1) "3"
 }
 [1] => array(3) {
   ["cxcs"] => int(73)
   ["juxiang"] => string(6) "上海"
   ["jieguo"] => string(1) "3"
 }
 [2] => array(3) {
   ["cxcs"] => int(73)
   ["juxiang"] => string(6) "上海"
   ["jieguo"] => string(1) "3"
 }
}第二个数组
array(3) {
 [0] => array(3) {
   ["juxiang"] => string(9) "北京"
 }
 [1] => array(3) {
   ["juxiang"] => string(6) "上海"
 }
 [2] => array(3) {
   ["juxiang"] => string(6) "广州"
 }
}1、首先合并第一个数组中["juxiang"]相同的,但是合并的时候要根据["jieguo"],把 ["cxcs"]改名为["cxcs"]+["jieguo"],例如:["jieguo"] => string(1) "4" ,合并过去的 ["cxcs"]键名为 ["cxcs4"];如果没有的值全部为0,但是["cxcs"]到 ["cxcs7"]要全部有;
2、根据第二个数组,如果第一个数组中没有,所有的["cxcs"]全部为0.
要得到的结果
array(3) {
 [0] => array(3) {
   ["cxcs"] => int(77)
   ["cxcs2"] => int(5)
   ["cxcs3"] => int(3)
   ["cxcs4"] => int(2)
   ["cxcs5"] => int(11)
   ["cxcs6"] => int(7)
   ["juxiang"] => string(9) "北京"
   ["jieguo"] => string(1) "3"
 }
 [1] => array(3) {
   ["cxcs"] => int(77)
   ["cxcs2"] => int(5)
   ["cxcs3"] => int(3)
   ["cxcs4"] => int(2)
   ["cxcs5"] => int(11)
   ["cxcs6"] => int(7)
   ["juxiang"] => string(6) "上海"
   ["jieguo"] => string(1) "3"
 }
 [2] => array(3) {
   ["cxcs"] => int0)
   ["cxcs2"] => int(0)
   ["cxcs3"] => int(0)
   ["cxcs4"] => int(0)
   ["cxcs5"] => int(0)
   ["cxcs6"] => int(0)
   ["juxiang"] => string(6) "广州"
   ["jieguo"] => string(1) "3"
 }
}

解决方案 »

  1.   

    1、给出数组的 var_export 打印结果
    2、给出实例数据处理后的正确结果
      

  2.   

    但是["cxcs"]到 ["cxcs7"]要全部有,你给的答案都没有cxcs7。[img=https://forum.csdn.net/PointForum/ui/scripts/csdn/Plugin/003/monkey/1.gif][/img
    按所讲的规则,使用给出的数据,不能得到想要的结果。你还是先整理下原数组,和最后得到的数组,规则再说明白点吧。
      

  3.   

    写错了,是["cxcs1"]到 ["cxcs6"]!
    第一个数组
    array(3) {
     [0] => array(3) {
       ["cxcs"] => int(77)
       ["juxiang"] => string(9) "北京"
       ["jieguo"] => string(1) "3"
     }
     [1] => array(3) {
       ["cxcs"] => int(50)
       ["juxiang"] => string(6) "上海"
       ["jieguo"] => string(1) "3"
     }
     [2] => array(3) {
       ["cxcs"] => int(73)
       ["juxiang"] => string(6) "上海"
       ["jieguo"] => string(1) "4"
     }
    }
    1、上面数组中 ["juxiang"] 的键值有两个是“上海”,首先把这两个“上海”合并,也就是说数组中["juxiang"]有相同键值的需要合并,合并只保留 ["juxiang"]  ["cxcs"] 。
    2、但是合并后 ["cxcs"]需要改名 ,需要根据["jieguo"] 的键值进行键名的修改,比如["jieguo"] 为4合并过去后 ["cxcs"]改为 ["cxcs4"] ,为3合并过去后 ["cxcs"]改为 ["cxcs3"] 。
    3、合并后必须有 ["cxcs1"]到 ["cxcs6"],如果合并的时候["jieguo"] 中没有的要补全,键值为0,比如合并的时候只有["cxcs3"]["cxcs4"],那么["cxcs1"]["cxcs2"]["cxcs5"]["cxcs6"]都为0;
    比如上面的上海这个合并后:
     [2] => array(3) {
       ["cxcs1"] => int(0)
       ["cxcs2"] => int(0)
       ["cxcs3"] => int(50)
       ["cxcs4"] => int(73)
       ["cxcs5"] => int(0)
       ["cxcs6"] => int(0)
       ["juxiang"] => string(6) "上海"
       ["jieguo"] => string(1) "4"
     }第一个数组合并后的最终结果:
    新的第一个数组
    array(2) {
     [0] => array(8) {
       ["cxcs1"] => int(0)
       ["cxcs2"] => int(0)
       ["cxcs3"] => int(77)
       ["cxcs4"] => int(0)
       ["cxcs5"] => int(0)
       ["cxcs6"] => int(0)
       ["juxiang"] => string(9) "北京"
     }
     [1] => array(8) {
       ["cxcs1"] => int(0)
       ["cxcs2"] => int(0)
       ["cxcs3"] => int(50)
       ["cxcs4"] => int(73)
       ["cxcs5"] => int(0)
       ["cxcs6"] => int(0)
       ["juxiang"] => string(6) "上海"
     }
    }
    1、新的第一个数组与下面的第二个数组合并,如果新的第一个数组 ["juxiang"] 中有的,保持不变,比如北京、上海第一个数组中有,就保持不变,而广州没有,就追加进去;追加进去的广州,必须和新的第一个数组中的一样,包含["cxcs1"]到 ["cxcs6"]和  ["juxiang"] ,["cxcs1"]到 ["cxcs6"]的值为0;
    第二个数组
    array(3) {
     [0] => array(0) {
       ["juxiang"] =>  "北京"
     }
     [1] => array(0) {
       ["juxiang"] =>  "上海"
     }
     [2] => array(0) {
       ["juxiang"] =>  "广州"
     }
    }
    合并后的数组结果
    array(3) {
     [0] => array(8) {
       ["cxcs1"] => int(0)
       ["cxcs2"] => int(0)
       ["cxcs3"] => int(77)
       ["cxcs4"] => int(0)
       ["cxcs5"] => int(0)
       ["cxcs6"] => int(0)
       ["juxiang"] => string(9) "北京"
     }
     [1] => array(8) {
       ["cxcs1"] => int(0)
       ["cxcs2"] => int(0)
       ["cxcs3"] => int(50)
       ["cxcs4"] => int(73)
       ["cxcs5"] => int(0)
       ["cxcs6"] => int(0)
       ["juxiang"] => string(6) "上海"
     }
     [1] => array(8) {
       ["cxcs1"] => int(0)
       ["cxcs2"] => int(0)
       ["cxcs3"] => int(0)
       ["cxcs4"] => int(0)
       ["cxcs5"] => int(0)
       ["cxcs6"] => int(0)
       ["juxiang"] => string(6) "广州"
     }
    }
      

  4.   

    让你贴出数组的 var_export 打印的结果就那么难吗?
    你这 var_dump 的结果,得修改多少才能拿来用?(与人方便,擦能与己方便)
    $d = array(
       "cxcs1" => 0,
       "cxcs2" => 0,
       "cxcs3" => 0,
       "cxcs4" => 0,
       "cxcs5" => 0,
       "cxcs6" => 0,
       "juxiang" => "",
    );
    $res = array();
    foreach($a as $v) {
      if(!isset($res[$v['juxiang']])) $res[$v['juxiang']] = $d;
      $res[$v['juxiang']]['juxiang'] = $v['juxiang'];
      $res[$v['juxiang']]['cxcs' . $v['jieguo']] = $v['cxcs'];
    }
    foreach($b as $v) {
      if(!isset($res[$v['juxiang']])) $res[$v['juxiang']] = $d;
      $res[$v['juxiang']]['juxiang'] = $v['juxiang'];
    }
    $res = array_value($res);