array( &"/> array( &">
Rumah >pembangunan bahagian belakang >tutorial php >问个PHP数组合并的有关问题~请
问个PHP数组合并的问题~请高手指点
$arr1=array(<br /> "0"=>array(<br /> 'id' => '2',<br /> 'b' => 0,<br /> 'num' => 4<br /><br /> ),<br /><br /> "1"=>array(<br /> 'id' => '1',<br /> 'b' => 0,<br /> 'num' => 4 <br /> )<br /> );<br /> $arr2=array(<br /> "1"=>array(<br /> 'id' => '3',<br /> 'b' => 0,<br /> 'num' => 4<br /><br /> ),<br /><br /> "2"=>array(<br /> 'id' => '2',<br /> 'b' => 0,<br /> 'num' => 4 <br /> )<br /> );
$arr1=array(<br /> "0"=>array(<br /> 'id' => '2',<br /> 'b' => 0,<br /> 'num' => 4<br /> <br /> ),<br /> <br /> "1"=>array(<br /> 'id' => '1',<br /> 'b' => 0,<br /> 'num' => 4 <br /> )<br /> );<br /> $arr2=array(<br /> "1"=>array(<br /> 'id' => '3',<br /> 'b' => 0,<br /> 'num' => 4<br /> <br /> ),<br /> <br /> "2"=>array(<br /> 'id' => '2',<br /> 'b' => 0,<br /> 'num' => 4 <br /> )<br /> );<br />foreach($arr1 as $v) $res["$v[id]_$v[b]"] = $v;<br />foreach($arr2 as $v)<br /> if(isset($res["$v[id]_$v[b]"])) $res["$v[id]_$v[b]"]['num'] += $v['num'];<br /> else $res["$v[id]_$v[b]"] = $v;<br />print_r(array_values($res));
Array<br />(<br /> [0] => Array<br /> (<br /> [id] => 2<br /> [b] => 0<br /> [num] => 8<br /> )<br /><br /> [1] => Array<br /> (<br /> [id] => 1<br /> [b] => 0<br /> [num] => 4<br /> )<br /><br /> [2] => Array<br /> (<br /> [id] => 3<br /> [b] => 0<br /> [num] => 4<br /> )<br /><br />)<br /><br />