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开发安全问题的相关总结

对于互联网应用的开发,作为开发者必须时刻牢记安全观念,并在开发的代码中体现。PHP脚本语言对安全问题并不太关心,特别是对大多数没有经验的开发者来说。每当你做任何涉及到钱财事务等交易问题时...

PHP编程实现微信企业向用户付款的方法示例

本文实例讲述了PHP编程实现微信企业向用户付款的方法。分享给大家供大家参考,具体如下: <?php header('content-type:text/html;cha...

PHP连接SQLSERVER 注意事项(附dll文件下载)

环境: - Apache 2.2.6 - PHP 5.2.5 - SQL Server 2005 - Windows XP SP2 步骤: 1. 首先按通常做法配置好PHP5连接MS S...

php事务回滚简单实现方法示例

本文实例讲述了php事务回滚简单实现方法。分享给大家供大家参考,具体如下: $servername="localhost"; $username="root"; $password=...

PHP实现生成唯一会员卡号

PHP实现生成唯一会员卡号

在不查询数据库的情况下,每个会员登录进来会生成一个数字字母组合不重复的会员卡号。 效果图如下: 当我们要将一个庞大的数据进行编号时,而编号有位数限制,比如5位的车牌号、10位的某证件号...