# php合并数组时array\_merge和+号的区别

**URL:** <https://www.shiqidu.com/t/topic/285>\
**Category:** 开发调优\
**Tags:** PHP\
**Created:** [2017 年11 月 14 日 12:07 UTC](https://www.shiqidu.com/t/topic/285 "2017-11-14T12:07:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2017 年11 月 14 日 12:07 UTC](https://www.shiqidu.com/t/topic/285/1 "2017-11-14T12:07:10Z")

</div>

大家都知道合并数组常用的两种方式`array_merge` 和 `+`

但是二者是有区别的，用到的时候要小心了，这里是有坑的，比如下面的代码

```php
$a = ['zhangsan', 'lisi'];  
$b = ['wangwu', 'zhaoliu'];  
  
$r1 = $a + $b;  
$r2 = array_merge($a, $b);  
  
var_dump($r1);  
var_dump($r2);  

```

输出结果

```php
# $r1  
array (size=2)  
  0 =&gt; string 'zhangsan' (length=8)  
  1 =&gt; string 'lisi' (length=4)  
# $r2  
array (size=4)  
  0 =&gt; string 'zhangsan' (length=8)  
  1 =&gt; string 'lisi' (length=4)  
  2 =&gt; string 'wangwu' (length=6)  
  3 =&gt; string 'zhaoliu' (length=7)  

```

为了检验二者的区别，在看下面的代码。

```php
$a = ['name' =&gt; 'zhangsan', 'age' =&gt; 24];  
$b = ['name' =&gt; 'lisi', 'sex' =&gt; 'male', 'city' =&gt; 'shanghai'];  
  
$r1 = $a + $b;  
$r2 = array_merge($a, $b);  
  
var_dump($r1);  
var_dump($r2);  

```

输出结果

```php
# $r1  
array (size=4)  
  'name' =&gt; string 'zhangsan' (length=8)  
  'age' =&gt; int 24  
  'sex' =&gt; string 'male' (length=4)  
  'city' =&gt; string 'shanghai' (length=8)  
# $r2  
array (size=4)  
  'name' =&gt; string 'lisi' (length=4)  
  'age' =&gt; int 24  
  'sex' =&gt; string 'male' (length=4)  
  'city' =&gt; string 'shanghai' (length=8)  

```

看到这是不是明白了一点

**在数组为索引数组(即键值为数字)时，用 `+` 合并数组时，`+` 前的数组会把 `+` 后的数组中拥有相同键的值覆盖掉；而`array_merge`则不会覆盖拥有相同键的值，并且会重新生成新的键。**

**在数组为关联数组(即键值为字符串)时，用 `+` 合并数组时，`+` 前的数组会把 `+` 后的数组中拥有相同键的值覆盖掉；而`array_merge`合并数组时，参数后面的数组会把前面的数组中拥有相同键的值覆盖掉。**

为了验证我们的猜想，再看下面的例子。

```php
$a = [0 =&gt; 'profile_a', 'name' =&gt; 'zhangsan', 'age' =&gt; 24, 'city' =&gt; 'beijing'];  
$b = [0 =&gt; 'profile_b', 1 =&gt; 'this is zhangsan`s profile', 'sex' =&gt; 'male', 'city' =&gt; 'shanghai'];  
  
$r1 = $a + $b;  
$r2 = array_merge($a, $b);  
  
var_dump($r1);  
var_dump($r2);  

```

输出结果

```php
# $r1  
array (size=6)  
  0 =&gt; string 'profile_a' (length=9)  
  'name' =&gt; string 'zhangsan' (length=8)  
  'age' =&gt; int 24  
  'city' =&gt; string 'beijing' (length=7)  
  1 =&gt; string 'this is zhangsan`s profile' (length=26)  
  'sex' =&gt; string 'male' (length=4)  
  
# $r2  
array (size=7)  
  0 =&gt; string 'profile_a' (length=9)  
  'name' =&gt; string 'zhangsan' (length=8)  
  'age' =&gt; int 24  
  'city' =&gt; string 'shanghai' (length=8)  
  1 =&gt; string 'profile_b' (length=9)  
  2 =&gt; string 'this is zhangsan`s profile' (length=26)  
  'sex' =&gt; string 'male' (length=4)  

```

\*\* 当数组中的键既有数字又有字符串时，使用 `+` 合并数组依然是前边的数组覆盖后面数组中拥有想似键的值，`array_merge` 则会保留合并键为数字的值，覆盖键为字符的值(后面的覆盖前面的) 。\*\*

php的数组过于灵活了，带来便利的同时也要注意一些坑。

\> **测试环境**  
\> php v 7.0

---

<div class="post-metadata">

作者： ![lisgroup](https://www.shiqidu.com/user_avatar/www.shiqidu.com/lisgroup/32/577_2.png) [@lisgroup](https://www.shiqidu.com/u/lisgroup)\
发布日期： [2017 年11 月 19 日 02:24 UTC](https://www.shiqidu.com/t/topic/285/2 "2017-11-19T02:24:28Z")

</div>

不错，感谢分享

---

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2017 年11 月 19 日 03:50 UTC](https://www.shiqidu.com/t/topic/285/3 "2017-11-19T03:50:45Z")

</div>

@lisgroup: 多谢支持。

---

<div class="post-metadata">

作者： ![jiong](https://www.shiqidu.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jiong](https://www.shiqidu.com/u/jiong)\
发布日期： [2018 年7 月 3 日 07:32 UTC](https://www.shiqidu.com/t/topic/285/4 "2018-07-03T07:32:56Z")

</div>

特点注册一个账号来感谢你 写的很棒很细致

---

<div class="post-metadata">

作者： ![jiong](https://www.shiqidu.com/letter_avatar_proxy/v4/letter/j/ecc23a/32.png) [@jiong](https://www.shiqidu.com/u/jiong)\
发布日期： [2018 年7 月 3 日 07:33 UTC](https://www.shiqidu.com/t/topic/285/5 "2018-07-03T07:33:45Z")

</div>

可以转载吗 会标注转载和作者的

---

<div class="post-metadata">

作者： ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
发布日期： [2018 年7 月 3 日 12:25 UTC](https://www.shiqidu.com/t/topic/285/6 "2018-07-03T12:25:58Z")

</div>

可以
