PHP array 的加法操作代码

yipeiwu_com6年前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流Streams、包装器wrapper概念与用法实例详解

本文实例讲述了PHP流Streams、包装器wrapper概念与用法。分享给大家供大家参考,具体如下: 流Streams这个概念是在php4.3引进的,是对流式数据的抽象,用于统一数据操...

php中检查文件或目录是否存在的代码小结

下面是一个简单的检查文件是否存在的实例代码: 复制代码 代码如下: <?php $filename = '/path/to/foo.txt'; if (file_exists($f...

php获取百度收录、百度热词及百度快照的方法

本文实例讲述了php获取百度收录、百度热词及百度快照的方法。分享给大家供大家参考。具体如下: 获取百度收录: <?php /* 抓取百度收录代码 */ function...

php实现每天自动变换随机问候语的方法

本文实例讲述了php实现每天自动变换随机问候语的方法。分享给大家供大家参考。具体分析如下: 这里预先定义一个php数组,里面存放一些随机问候语,调用的时候指定是按照天,月还是年来自动更换...

生成ubuntu自动切换壁纸xml文件的php代码

复制代码 代码如下: <?php /* * 生成ubuntu自动切换壁纸xml文件 */ //图片目录 $dir = '/home/yuxing/background'; $hd...