2

I have problem wiht array_merge():

First array:

$array1=array(
[0]=>array(["key1"]=>"value1",["key2"]=>"value2",["key3"]=>"value3")
);

Second array:

$array2=array(["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6");

And I need merge this arrays to one like this:

$array1=array(
    [0]=>array(["key1"]=>"value1",["key2"]=>"value2",
    ["key3"]=>"value3",["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6"));

But when use:

$array3=array_merge($array1,$array2);
var_dump($array3);

var_dump return this:

 array(
    [0]=>array(["key1"]=>"value1",["key2"]=>"value2",
    ["key3"]=>"value3") ["key4"]=>"value4",["key5"]=>"value5",["key6"]=>"value6");

And don't know why.

Thanks

1
  • Then you just want to merge the first subArray from the first array with the second array, nothing special. Commented Aug 25, 2016 at 20:40

2 Answers 2

1
$array3=array(array_merge($array1[0],$array2));

you have to merge the inner array, not the outer one.

https://3v4l.org/dCm2F

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

Comments

1

Merging the first element from first array with the second one, may help:

$array3 = array();
$array3[0] = array_merge($array1[0], $array2);

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.