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函数array_filter() . array array_filter ( array [, callback callback] ) ar...

php获取网页里所有图片并存入数组的方法

本文实例讲述了php获取网页里所有图片并存入数组的方法。分享给大家供大家参考。具体如下: $images = array(); preg_match_all('/(img|src)=...

PHP XML操作类DOMDocument

DOMDocument相关的内容. 属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataType 返回此节点的数据类型...

php基于curl重写file_get_contents函数实例

本文实例讲述了php基于curl重写file_get_contents函数。分享给大家供大家参考,具体如下: file_get_contents在连接不上的时候会提示Connection...

php的chr和ord函数实现字符加减乘除运算实现代码

chr函数用于将ASCII码转换为字符 ord函数是用来字符转换为ASCII码 ASCII码是计算机所能显示字符的编码,它的取值范围是0-255,其中包括标点、字母、数字、汉字等。在编程...