PHP array 的加法操作代码

yipeiwu_com5年前PHP代码库

The + operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten.

今天 再次看 php manual的时候,才知道

复制代码 代码如下:

<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
?>


When executed, this script will print the following:
Union of $a and $b:
复制代码 代码如下:

array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Union of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}

原来,我的理解就是。直接把$b中的元素直接复制到$a中。
我错了。

相关文章

PHP imagecreatefrombmp 从BMP文件或URL新建一图像

大家都知道php GD库可方便的从URL新建一图像, GD中有imagecreatefromjpeg(),imagecreatefromPNG()....等之类的FUNCTION 可有时...

php优化及高效提速问题的实现方法第1/2页

一、 在函数中,传递数组时使用 return 比使用 global 要高效,比如: function userloginfo($usertemp){ $detail=explode("|...

php冒泡排序与快速排序实例详解

本文实例分析了php冒泡排序与快速排序算法。分享给大家供大家参考,具体如下: $a=array('3','8','1','4','11','7'); print_r($a); $le...

PHP表单提交表单名称含有点号(.)则会被转化为下划线(_)

PHP表单提交表单名称含有点号(.)则会被转化为下划线(_)

最近在做公司项目的时候,发现一个奇怪的问题,递交一个正常表单,竟然发现不能正常获取到递交的值,这一发现,不免让我开始的时候一头雾水,开始的时候一度认为是我的服务有问题,不能正常的写入数据...

使用PHP函数scandir排除特定目录

scandir()函数返回一个数组,其中包含指定路径中的文件和目录。如下所示: 例子: 复制代码 代码如下:<?phpprint_r(scandir('test_directory...