如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
键名相同且必须是字符
$a = array("w"=>1,1,2,3,4); $b = array("w"=>2,1,2,3,5); $a = array_merge($a,$b); var_dump($a);
这样写被不被认为是字符,且还会在后面添加
$a = array("1"=>1,1,2,3,4); $b = array("1"=>2,1,2,3,5); $a = array_merge($a,$b); var_dump($a);
案例
<?php $school=[ 'class'=>[ '一班'=>[ 'name'=>['小明','小红','小刚'], 'sex'=>['男','女','男'], ], '三班'=>[ 'name'=>['小步','小心','小美'], 'sex'=>['男','女','女'], ] ], ]; $fu=[ '一班'=>[ 'name'=>['小白'], 'sex'=>['男'], ], '三班'=>[ 'name'=>['小可'], 'sex'=>['女'], ], ]; //先判断是否是一个班级的 //然后按名字覆盖 //性别覆盖 foreach($school['class'] as $k1=>$vo1){ foreach($fu as $k2=>$vo2){ if($k1==$k2){ $school['class'][$k2]['name'] =array_merge($vo1['name'],$vo2['name']); $school['class'][$k2]['sex'] = array_merge($vo1['sex'],$vo2['sex']); }else{ } } } echo "<pre>"; var_dump($school['class']);