0

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?

2
  • What exactly you mean by comparing keys? Commented Aug 11, 2015 at 5:07
  • 1
    i am not sure what exactly you want but print_r(array_merge($arr1['someKey'],$arr2['someKey'])); will work Commented Aug 11, 2015 at 5:09

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

please check on your way should work.

$arr3 = $arr1 + $arr2; 

print_r($arr3);

as same as array_merge but preserved array keys.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.