Home > php教程 > php手册 > body text

PHP 数组合并相加与array_merge 的区别

WBOY
Release: 2016-06-13 10:17:07
Original
1286 people have browsed it

在php中数组合并相加与array_merge数组合并是有一点区别的,下面小编来给各位同学介绍array_merge与数组相加合并的区别。


+ :

首先出现的会覆盖后出现的


array_merge:

后出现的会覆盖前面出现的。但是如果是数字索引,则是叠加效果。

 代码如下 复制代码

$array1 = Array(
    0 => 111
);

$array2 = Array(
    0 => 222,
    1 => 3333
);

array_merge 后:
Array
(
    [0] => 111
    [1] => 222
    [2] => 3333
)

+ 后:
Array
(
    [0] => 111
    [1] => 3333
)

情况二:

$array1 = Array(
    0 => 111
);

$array2 = Array(
    'h' => 222,
    1 => 3333
);
array_merge 和 + 的结果是一样:
Array
(
    [0] => 111
    [h] => 222
    [1] => 3333
)


结果很明显:

array_merge就是两个数组的相加,键值如果是数字的话 会重新排列 如果不的话是不会修改
而+是两个数组相加会把相同的键值的值会被前面的数组给替换,如果不一样的键值那就和array_merge一样的效果.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!