php array_merge()函数

当数组中索引都是数字索引时,会把数组重新从第一个数组开始,并索引从0开始生成新的数组
$a1=array("1"=>"red","2"=>"green");
$a2=array("2"=>"blue","3"=>"yellow","4"=>"reddd");
print_r(array_merge($a1,$a2));

Array(    [0] => red    [1] => green    [2] => blue    [3] => yellow    [4] => reddd)

当数组索引是非数字索引时,后面的数组元素会替换前面的

$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow","a"=>"reddd");
print_r(array_merge($a1,$a2));
Array
(
    [a] => reddd
    [b] => yellow
    [c] => blue
)


当数组中有数字也有非数字时,数字索引会和非数字索引一样,后面替换前面的

$a1=array("a"=>"red","b"=>"green","1"=>"haha");
$a2=array("c"=>"blue","b"=>"yellow","a"=>"reddd","1"=>"hehe");
print_r(array_merge($a1,$a2));
Array
(
    [a] => reddd
    [b] => yellow
    [0] => haha
    [c] => blue
    [1] => hehe
)
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页