php数组求和array_merge和+号 的区别

<?php
$d1=array(
	'2015-6-1'=>'0',
	'2015-6-2'=>'0',
	'2015-6-3'=>'0',
	'2015-6-4'=>'0',
	'2015-6-5'=>'100',
	'2015-6-6'=>'0',
	'2015-6-7'=>'end',
	'theend'=>'end!',
	'1'=>'the one',
	'21'=>'the three'
	);

$d2=array(
	'2015-6-1'=>'9',
	'2015-6-2'=>'8',
	'2015-6-3'=>'7',
	'2015-6-4'=>'6',
	'2015-6-5'=>'5',
	'2015-6-6'=>'4',
	'1'=>'the two'

);
var_dump($d1+$d2);
var_dump(array_merge($d1,$d2));
// var_dump($days);

result:

array(10) {
  ["2015-6-1"]=>
  string(1) "0"
  ["2015-6-2"]=>
  string(1) "0"
  ["2015-6-3"]=>
  string(1) "0"
  ["2015-6-4"]=>
  string(1) "0"
  ["2015-6-5"]=>
  string(3) "100"
  ["2015-6-6"]=>
  string(1) "0"
  ["2015-6-7"]=>
  string(3) "end"
  ["theend"]=>
  string(4) "end!"
  [1]=>
  string(7) "the one"
  [21]=>
  string(9) "the three"
}



array(11) {
  ["2015-6-1"]=>
  string(1) "9"
  ["2015-6-2"]=>
  string(1) "8"
  ["2015-6-3"]=>
  string(1) "7"
  ["2015-6-4"]=>
  string(1) "6"
  ["2015-6-5"]=>
  string(1) "5"
  ["2015-6-6"]=>
  string(1) "4"
  ["2015-6-7"]=>
  string(3) "end"
  ["theend"]=>
  string(4) "end!"
  [0]=>
  string(7) "the one"
  [1]=>
  string(9) "the three"
  [2]=>
  string(7) "the two"
}
[Finished in 0.9s]


1、两个数组相加:(+):是用第一个数组覆盖第二个数组中键名字相同的。数字索引和字符串索引(关联数组)都会被覆盖,而且数字索引下标保留。

2、array_merge:是第二个数组覆盖第一个数组键名相同。数字索引会被保留(+会被直接覆盖掉),而且数字索引被重新排序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值