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+jQuery实现双击修改table表格功能示例

PHP+jQuery实现双击修改table表格功能示例

本文实例讲述了PHP+jQuery实现双击修改table表格功能。分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"&...

PHP实现UTF8二进制及明文字符串的转化功能示例

本文实例讲述了PHP实现UTF8二进制及明文字符串的转化功能。分享给大家供大家参考,具体如下: <?php /***********本程序由云客编写。有空的时候承接ph...

php从身份证获取性别和出生年月

话不多说,请看代码: //通过身份证号查询出性别与生日 $birth = strlen($idcard)==15 ? ('19' . substr($idcard, 6,...

PHP cron中的批处理

大型的连锁店有一个大问题。每天,在每家商店会发生数千次交易。公司执行官希望对这些数据进行挖掘。哪些产品卖得好?哪些不好?有机产品在哪里卖得好?冰淇淋的销售情况怎么样? 为了捕捉这些数据...

php 启动报错如何解决

复制代码 代码如下: [root@abc lnmp]# service php-fpm start Starting php-fpm eAccelerator: Could not al...