"aa","a1"=>1,"a2"=>1),array("a"=>"bb","a1"=>2,"a2"=>2),array("a"=>"cc","a1"=>3,"a2"=>3),arra">
时间:2021-07-01 10:21:17 帮助过:29人阅读
$a = array( array("a" => "aa", "a1" => 1, "a2" => 1), array("a" => "bb", "a1" => 2, "a2" => 2), array("a" => "cc", "a1" => 3, "a2" => 3), array("a" => "aa", "a1" => 1, "a2" => 1), array("a" => "bb", "a1" => 5, "a2" => 5),);$res = array();foreach($a as $r) { if(! isset($res[$r['a']])) $res[$r['a']] = $r; else { $res[$r['a']]['a1'] += $r['a1']; $res[$r['a']]['a2'] += $r['a2']; }}print_r(array_values($res));
Array( [0] => Array ( [a] => aa [a1] => 2 [a2] => 2 ) [1] => Array ( [a] => bb [a1] => 7 [a2] => 7 ) [2] => Array ( [a] => cc [a1] => 3 [a2] => 3 ))