/** * 二维数组相同数据格式做合并,并根据字段做排序操作 * 打印,$json_arr1 OR $json_arr2 ,看数据结构 * @return array */ $json_1 = '[{"months":"2017\u5e7407\u6708\u8d26\u5355","t_months":"2017-07","moneys":"1539.43","balances":"Y,Y,Y,Y,Y,Y,Y,Y,Y,Y,Y,Y,Y,Y,Y"},{"months":"2017\u5e7408\u6708\u8d26\u5355","t_months":"2017-08","moneys":"598.00","balances":"Y,Y"},{"months":"2017\u5e7411\u6708\u8d26\u5355","t_months":"2017-11","moneys":"62.74","balances":"Y"}]'; $json_2 = '[{"months":"2017\u5e7408\u6708\u8d26\u5355","t_months":"2017-08","moneys2":"23473.09","balances":"N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N"},{"months":"2017\u5e7409\u6708\u8d26\u5355","t_months":"2017-09","moneys2":"8614.42","balances":"N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N"},{"months":"2017\u5e7410\u6708\u8d26\u5355","t_months":"2017-10","moneys2":"8455.06","balances":"N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N,N"},{"months":"2017\u5e7411\u6708\u8d26\u5355","t_months":"2017-11","moneys2":"86.85","balances":"N,N"}]'; $json_arr1 = json_decode($json_1, true); $json_arr2 = json_decode($json_2, true); $data_1 = []; $data_2 = []; //todo : 数量小的months做key $replace_data = count($json_arr1) < count($json_arr2) ? $json_arr1 : $json_arr2; foreach ($replace_data as $k=>$a) { $data_1[$a['months']] = $a; } //todo: 数组合并,并根据根据key循环做存已存在和未存,数据重组操作 foreach (array_merge($json_arr1,$json_arr2) as $ks => $b) { unset($b['balances']); $data_2[$b['months']] = $b; isset($data_1[$b['months']]) and $data_2[$b['months']] = [ 'months' => $b['months'], 't_months'=> $b['t_months'], 'moneys' => !empty($data_1[$b['months']]['moneys']) ? $data_1[$b['months']]['moneys'] : 0, 'moneys2' => !empty($b['moneys2']) ? $b['moneys2'] : 0, ]; } //todo: 二维数组根据某个字段做排序操作(只限制数值) $data_arr = array_values($data_2); $arr_sort = []; foreach($data_arr as $kr => $value){ foreach($value as $k=>$v){ $arr_sort[$k][$kr] = $v; } } array_multisort($arr_sort['t_months'], SORT_ASC, $data_arr); print_r($data_arr);