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时区转换转换函数

复制代码 代码如下:/* * 时区转换 */function toTimeZone($src, $from_tz = 'America/Denver', $to_tz...

php读取富文本的时p标签会出现红线是怎么回事

php读取富文本的时p标签会出现红线是怎么回事

为什么php读取富文本的时候,p标签会出现红线,怎么去掉,哪位大侠帮解决?跪求答案快哭了 就像下面一样,一遇到p标签就有红虚线...

PHP中多维数组的foreach遍历示例

复制代码 代码如下: <?php //声明一个三维数组 $info=array( "user"=>array( array(1,"zhangsan",20,"nan"), a...

php 静态属性和静态方法区别详解

php 静态属性和静态方法区别详解

静态 public、private、protected 静态属性     在属性或方法前加static关键字,即为静态属性   ...

php简单创建zip压缩文件的方法

本文实例讲述了php简单创建zip压缩文件的方法。分享给大家供大家参考,具体如下: /* creates a compressed zip file */ function crea...