简体   繁体   中英

Array_merge() two array into one not working

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

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

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

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

https://3v4l.org/dCm2F

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM