通过键比较将数组值附加到另一个数组

通过键比较将数组值附加到另一个数组

问题描述:

I've this two arrays:

$arr1['someKey'] = [1,2,3,4,5];
$arr2['someKey'] = [6,7];

how do I add|append the values from the second one into the first one by comparing it's keys? The result should be something like:

$arr3['someKey'] = [1,2,3,4,5,6,7];

Any help?

我有这两个数组: p>

  $ arr1 [  'someKey'] = [1,2,3,4,5]; 
 $ arr2 ['someKey'] = [6,7]; 
  code>  pre> 
 
 

如何通过比较它的键来将第二个值添加到第一个值? 结果应该是这样的: p>

  $ arr3 ['someKey'] = [1,2,3,4,5,6,7]; 
  code  >  pre> 
 
 

任何帮助? p> div>

Try array_merge_recursive:

$arr1 = array(
    'someKey' => [1,2,3,4,5],
);
$arr2 = array(
    'someKey' => [6,7],
);

$merged = array_merge_recursive($arr1, $arr2);

Ideone: http://ideone.com/0wfez8

Try this..

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

$arr3 = array_merge_recursive($arr1, $arr2);
print_r($arr3);

http://php.net/manual/en/function.array-merge-recursive.php

please check on your way should work.

$arr3 = $arr1 + $arr2; 

print_r($arr3);

as same as array_merge but preserved array keys.