我有2个阵列
$a = array('v1'=>1,'v2'=>2,'v3'=>3,'v4'=>4); $b = array('v1'=>1,'v4'=>5);
如何将它们合并到单个数组中,如下所示:
Array ( [v1] => 1 [v2] => 2 [v3] => 3 [v4] => Array ( [0] => 4 [1] => 5 ) )
我尝试过使用array_merge& array_merge_recursive.
解决方法
您可以使用此代码:
$a = array('v1'=>1,'v4'=>4); $b = array('v1'=>1,'v4'=>5); $c = array(); foreach($a as $m => $n) { if (($b[$m] != $n)) $c[$m] = array($n,$b[$m]); else $c[$m] = $n; }