$arr1=array( "0"=>array( 'id' => '2', 'b' => 0, 'num' => 4 ), "1"=>array( 'id' => '1', 'b' => 0, 'num' => 4 ) ); $arr2=array( "1"=>array( 'id' => '3', 'b' => 0, 'num' => 4 ), "2"=>array( 'id' => '2', 'b' => 0, 'num' => 4 ) );
array_merge($fruits, $numbered);大丈夫です
お兄さん、条件に注意してください
$arr1=array( "0"=>array( 'id' => '2', 'b' => 0, 'num' => 4 ), "1"=>array( 'id' => '1', 'b' => 0, 'num' => 4 ) ); $arr2=array( "1"=>array( 'id' => '3', 'b' => 0, 'num' => 4 ), "2"=>array( 'id' => '2', 'b' => 0, 'num' => 4 ) );foreach($arr1 as $v) $res["$v[id]_$v[b]"] = $v;foreach($arr2 as $v) if(isset($res["$v[id]_$v[b]"])) $res["$v[id]_$v[b]"]['num'] += $v['num']; else $res["$v[id]_$v[b]"] = $v;print_r(array_values($res));
Array( [0] => Array ( [id] => 2 [b] => 0 [num] => 8 ) [1] => Array ( [id] => 1 [b] => 0 [num] => 4 ) [2] => Array ( [id] => 3 [b] => 0 [num] => 4 ))
$arr1=array( "0"=>array( 'id' => '2', 'b' => 0, 'num' => 4 ), "1"=>array( 'id' => '1', 'b' => 0, 'num' => 4 ) ); $arr2=array( "1"=>array( 'id' => '3', 'b' => 0, 'num' => 4 ), "2"=>array( 'id' => '2', 'b' => 0, 'num' => 4 ) );foreach($arr1 as $v) $res["$v[id]_$v[b]"] = $v;foreach($arr2 as $v) if(isset($res["$v[id]_$v[b]"])) $res["$v[id]_$v[b]"]['num'] += $v['num']; else $res["$v[id]_$v[b]"] = $v;print_r(array_values($res));
Array( [0] => Array ( [id] => 2 [b] => 0 [num] => 8 ) [1] => Array ( [id] => 1 [b] => 0 [num] => 4 ) [2] => Array ( [id] => 3 [b] => 0 [num] => 4 ))
読ませていただき、とても考え方が広がり、とても勉強になりました。