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 INT类型在内存中占字节详解

PHP INT类型在内存中占字节详解

本教程将介绍输出INT类型在内存中占多少个字节 新建一个333.php,如图所示: 添加php的界定符(<?php?>),如图所示: 声明PHP与浏览器交...

php魔术方法与魔术变量、内置方法与内置变量的深入分析

php内置变量了:DIRECTORY_SEPARATORDIRECTORY_SEPARATOR是一个返回跟操作系统相关的路径分隔符的php内置命令,在windows上返回/,而在linu...

php静态文件生成类实例分析

本文实例讲述了php静态文件生成类。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:defined('phpjb51') or die(header("http/1.1...

一个简单php扩展介绍与开发教程

一个简单php扩展介绍与开发教程

我们使用php扩展,主要目的是提高程序的执行效率,对于访问量很大的代码或者逻辑将其写成扩展。在做项目的过程中,需要对数据进行排序,数据运算比较复杂;我们准备对一百万个数据进行排序, 下面...

支持数组的ADDSLASHES的php函数

复制代码 代码如下://SQL ADDSLASHES function saddslashes($string) { if(is_array($string)) { foreach($s...