首頁 >後端開發 >php教程 >问个PHP数组合并的问题~请高手指点

问个PHP数组合并的问题~请高手指点

WBOY
WBOY原創
2016-06-23 13:33:17866瀏覽

$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                 )        );

想要合并这个两个数组,判断条件,id,b两个元素都相同的情况的NUM值相加。新数组键值重新排列0 1 2 3
求高手指点。谢谢。


回复讨论(解决方案)

array_merge($fruits, $numbered);  合并数组就行了啊

array_merge($fruits, $numbered);  合并数组就行了啊


大哥注意看条件
判断条件,id,b两个元素都相同的情况的NUM值相加。

有人在吗?

$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        ))


非常感谢,看了以后思路又开阔了不少,收获良多,再次谢谢大神解答。
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn