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 数组操作(增加,删除,查询,排序)等函数说明第1/2页

数据增加,删除,查询,排序详细说明 对数组的添加(首尾数据添加(不限定条数)以及中间任意位置添加数据操作) . 2~对数组的删除(首尾的数据删除(不限定条数)以及中间任意位置的数据删除操...

PHP swoole和redis异步任务实现方法分析

PHP swoole和redis异步任务实现方法分析

本文实例讲述了PHP swoole和redis异步任务实现方法。分享给大家供大家参考,具体如下: redis异步任务 interface.php <?php for($...

PHP实现多维数组转字符串和多维数组转一维数组的方法

本文实例讲述了PHP实现多维数组转字符串和多维数组转一维数组的方法。分享给大家供大家参考。具体实现方法如下: /** * @method 多维数组转字符串 * @param ty...

两级联动select刷新后其值保持不变的实现方法

背景 还是学院和专业的关系,我需要保持点击提交按钮后,页面select中继续维持提交前的值 网上有几种办法,要么通过url跳转时候附带参数形式传回页面,或者提交时候讲select的值保存...

php 保留小数点

$n=round(1.95583, 2); 这是四舍五入法保留2位小数...