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 Global变量定义当前页面的全局变量实现探讨

PHP Global变量在实际应用中会发现许多问题需要我们不断的去完善处理。我们在这篇文章中就针对PHP Global变量出现的问题给出了一些具体的解决办法。 1:PHP Global变...

thinkphp 验证码 的使用小结

 thinkphp中的验证码是可以直接调用的,非常方便,我们看一下 Think 文件夹下 有一个名为verify.class.php的文件    首先 我们...

PHP封装的简单连接MongoDB类示例

本文实例讲述了PHP封装的简单连接MongoDB类。分享给大家供大家参考,具体如下: 1. 封装MongoDB类 <?php class MongoDB { pri...

PHP处理JSON字符串key缺少双引号的解决方法

本文实例讲述了PHP处理JSON字符串key缺少引号的解决方法,分享给大家供大家参考之用。具体方法如下: 通常来说,JSON字符串是key:value形式的字符串,正常key是由双引号括...

PHP各版本中函数的类型声明详解

PHP7开始支持标量类型声明,强类型语言的味道比较浓。使用这个特性的过程中踩过两次坑:一次是声明boolean,最近是声明double。为避免以后继续犯类似错误,就把官方文档翻了一次。本...