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中把对象转换为关联数组代码分享

/** * 对象转关联数组 * @author * @param object $obj * @return array */ function object_to_a...

php线性表的入栈与出栈实例分析

本文实例讲述了php线性表的入栈与出栈用法。分享给大家供大家参考。具体如下: <?php $stack = array("Simon", "Elaine"); //定...

PHP函数microtime()用法与说明

定义和用法PHP函数microtime()返回当前 Unix 时间戳和微秒数。PHP函数microtime()语法microtime(get_as_float)PHP函数microtim...

自制PHP框架之模型与数据库

自制PHP框架之模型与数据库

什么是模型? 我们的WEB系统一定会和各种数据打交道,实际开发过程中,往往一个类对应了关系数据库的一张或多张数据表,这里就会出现两个问题。 1.类和数据表,一方修改会导致另一方的修改,只...

解析array splice的移除数组中指定键的值,返回一个新的数组

使用环境:人才网项目中有一个简历保密设置,其中有一个过滤关键词,只有某个企业的公司名中包含有其中的一个关键字,就不显示该份简历,当然,我还没有做到那里去,现在是要做关键词的增加删除。设想...