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中的动态调用实例分析

本文实例讲述了php中的动态调用具体用法。分享给大家供大家参考。具体分析如下: 在程序中如果加一大堆判断的确是一个很大的麻烦,例如这样: 复制代码 代码如下:if($fun='a'){e...

PHP实现搜索相似图片

感知哈希算法 count < =5 匹配最相似 count > 10 两张不同的图片 var_dump(ImageHash::run(‘./1.png', ‘./psb.jp...

PHP+Ajax实时自动检测是否联网的方法

本文实例讲述了PHP+Ajax实时自动检测是否联网的方法。分享给大家供大家参考。具体实现方法如下: html部分代码: <!DOCTYPE html PUBLIC "-//W3...

php实现URL加密解密的方法

本文实例讲述了php实现URL加密解密的方法。分享给大家供大家参考,具体如下: <html xmlns="http://www.w3.org/1999/xhtml" lang=...

PHP中类的继承和用法实例分析

本文实例讲述了PHP中类的继承和用法。分享给大家供大家参考,具体如下: 1、继承关键字 :extends PHP类的继承,我们可以理解成共享被继承类的内容。PHP中使用extends单一...