码迷,mamicode.com
首页 > Web开发 > 详细

PHP中array_merge()函数与array+array、array_merge_recursive() 的区别

时间:2018-12-01 13:16:21      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:数组元素   array   代码   gear   str   bsp   不同   ref   合并数组   

区别如下:

 当下标为数值时,array_merge()不会覆盖掉原来的值,但array+array合并数组则会把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉(不是覆盖). 

当下标为字符时,array+array仍然把最先出现的值作为最终结果返回,而把后面的数组拥有相同键名的那些值“抛弃”掉,但array_merge()此时会覆盖掉前面相同键名的值. 

该函数与array_merge_recursive() 函数之间的不同是在处理两个或更多个数组元素有相同的键名的情况。array_merge_recursive() 不会进行键名覆盖,而是将多个相同键名的值递归组成一个数组。

例子1:

代码:

$arr1 = [‘PHP‘, ‘apache‘];
$arr2 = [‘PHP‘, ‘MySQl‘, ‘HTML‘, ‘CSS‘];
$mergeArr = array_merge($arr1, $arr2);
$plusArr = $arr1 + $arr2;
var_dump($mergeArr);

var_dump($recurArr);
var_dump($plusArr);

结果
$mergeArr:

array (size=6)
  0 => string ‘PHP‘ (length=3)
  1 => string ‘apache‘ (length=5)
  2 => string ‘PHP‘ (length=3)
  3 => string ‘MySQl‘ (length=5)
  4 => string ‘HTML‘ (length=4)
  5 => string ‘CSS‘ (length=3)

$recurArr:

array (size=6)
  0 => string ‘PHP‘ (length=3)
  1 => string ‘apache‘ (length=5)
  2 => string ‘PHP‘ (length=3)
  3 => string ‘MySQl‘ (length=5)
  4 => string ‘HTML‘ (length=4)
  5 => string ‘CSS‘ (length=3)

$plusArr

array (size=4)
  0 => string ‘PHP‘ (length=3)
  1 => string ‘apache‘ (length=5)
  2 => string ‘HTML‘ (length=4)
  3 => string ‘CSS‘ (length=3

例子2:

代码:

$arr1 = [‘PHP‘, ‘a‘=>‘MySQl‘];
$arr2 = [‘PHP‘, ‘MySQl‘, ‘a‘=>‘HTML‘, ‘CSS‘];
$mergeArr = array_merge($arr1, $arr2);
$plusArr = $arr1 + $arr2;
var_dump($mergeArr);

var_dump($recurArr);
var_dump($plusArr);

结果
$mergeArr:

array (size=5)
  0 => string ‘PHP‘ (length=3)
  ‘a‘ => string ‘HTML‘ (length=4)
  1 => string ‘PHP‘ (length=3)
  2 => string ‘MySQl‘ (length=5)
  3 => string ‘CSS‘ (length=3)

$recurArr
array (size=6)
  0 => string ‘PHP‘ (length=3)
  ‘a‘ =>
      array(size=2)
           0 => string ‘MYSQl‘(length=5)
           1 => string ‘HTML‘(length=5)
  1 => string ‘PHP‘ (length=3)
  2 => string ‘MySQl‘ (length=5)
  3 => string ‘CSS‘ (length=3)

 

$plusArr

array (size=4)
  0 => string ‘PHP‘ (length=3)
  1 => string ‘a=>MySQl‘‘ (length=5)
  2 => string ‘a=>HTML‘‘ (length=5)
  3 => string ‘CSS‘ (length=3)

例子3:

代码:

$arr1 = [‘PHP‘, ‘a‘=>‘MySQl‘,‘6‘=>‘CSS‘];
$arr2 = [‘PHP‘, ‘MySQl‘, ‘a‘=>‘HTML‘, ‘CSS‘];
$mergeArr = array_merge($arr1, $arr2);

$recurArr = array_merge_recursive($arr1,$arr2);
$plusArr = $arr1 + $arr2;
var_dump($mergeArr);

var_dump($recurArr);
var_dump($plusArr);

结果:

$mergeArr

array (size=6)
  0 => string ‘PHP‘ (length=3)
  ‘a‘ => string ‘HTML‘ (length=4)
  1 => string ‘CSS‘ (length=3)
  2 => string ‘PHP‘ (length=3)
  3 => string ‘MySQl‘ (length=5)
  4 => string ‘CSS‘ (length=3)$recurArr

array (size=6)
  0 => string ‘PHP‘ (length=3)
  ‘a‘ =>
      array(size=2)
           0 => string ‘MYSQl‘(length=5)
           1 => string ‘HTML‘(length=5)
  1 => string ‘CSS‘ (length=3)
  2 => string ‘PHP‘ (length=3)
  3 => string ‘MySQl‘ (length=5)
  4 => string ‘CSS‘ (length=3)

$plusArr

array (size=5)
  0 => string ‘PHP‘ (length=3)
  ‘a‘ => string ‘MySQl‘ (length=5)
  6 => string ‘CSS‘ (length=3)
  1 => string ‘MySQl‘ (length=5)
  2 => string ‘CSS‘ (length=3)

相信通过上边三个例子大家已经非常清楚array_merge()函数和array+array数组相加的区别了吧。

PHP中array_merge()函数与array+array、array_merge_recursive() 的区别

标签:数组元素   array   代码   gear   str   bsp   不同   ref   合并数组   

原文地址:https://www.cnblogs.com/1301694f/p/10048662.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!