合并两个关联数组而不重新索引 - PHP

Merge two associative arrays without reindexing - PHP

如果不重新索引,我如何合并 2 个关联数组?我不想使用 foreach 或 for 循环,有更好的主意吗?

$a1=array(555=>array('name'=>'First Name','surname'=>'Second Name'), 888=>array('name'=>'New First Name','surname'=>'New Second Name'));
$a2=array(888=>array('invite'=>false), 555=>array('invite'=>true)); 
$result=array(555=>array('name'=>'First Name','surname'=>'Second Name', 'invite'=>true), 888=>array('name'=>'New First Name','surname'=>'New Second Name', 'invite'=>false));

使用array_merge函数或$a1 + $a2.

使用array_replace_recursive将邀请项添加到相应的源数组元素

array_replace_recursive($a1, $a2);

demo

你应该使用array_replace_recursive函数

$a1=array(555=>array('name'=>'First Name','surname'=>'Second Name'), 888=>array('name'=>'New First Name','surname'=>'New Second Name'));
$a2=array(888=>array('invite'=>false), 555=>array('invite'=>true));
print_r(array_replace_recursive($a1, $a2));

这将打印:

Array ( [555] => Array ( [name] => First Name [surname] => Second Name [invite] => 1 ) [888] => Array ( [name] => New First Name [surname] => New Second Name [invite] => ) )