1

I am trying to recursively merge two arrays using array_replace_recursive. This is the code:

$col = array();
$new = array_merge_recursive($col, array('table1' => array(1 => true)));
$new = array_merge_recursive($new, array('table1' => array(0 => false)));

The dump of the $new array is

array(1) { ["table1"]=> array(2) { [1]=> bool(true) [2]=> bool(false) } }

What i need is to preserve the numeric keys of the "table1" array. The expected result should be

array(1) { ["table1"]=> array(2) { [0]=> bool(false) [1]=> bool(true) } }

Does anyone have a solution for this?

2
  • Use array_replace_recursive instead of array_merge_recursive. Commented Dec 3, 2014 at 13:14
  • Thanks. This seems to work. Please, submit an answer to confirm. @ThinkDifferent Commented Dec 3, 2014 at 13:37

2 Answers 2

1

Use array_replace_recursive instead of array_merge_recursive.

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

Comments

0

You could switch the order of your arguments

$new = array_merge_recursive(array(), array('table1' => array(0 => false)));
$new = array_merge_recursive($new, array('table1' => array(1 => true)));

The problem you're running into is you already created a key so subsequent calls will append to the first element of the array.

1 Comment

Thanks for the answer. This works too, only that i am doing more than two merges with stored keys and it would imply sorting first, when array_replace_recursive does it without sorting

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.