PHP array 的加法操作代码

yipeiwu_com5年前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中。
我错了。

相关文章

深入for,while,foreach遍历时间比较的详解

这个是从别人空间里看来的,不过自己还真从来没这么做过他们三者之间的比较,今天也学习了一下。复制代码 代码如下:<?php$arr = array();for($i = 0; $i...

php相当简单的分页类

class Helper_Page{ /** 总信息数 */ var $infoCount; /** 总页数 */ var $pageCount; /** 每页显示条数 */ var $...

php的优点总结 php有哪些优点

php有哪些优点? PHP优点: 1.入门快,有其它语言基础的程序员二周左右的时间就可以入门,一个月左右的时间基本上就可以开发简单的项目了。 2.开发成本低,PHP最经典的组合就是:Li...

php实现SAE上使用storage上传与下载文件的方法

本文实例讲述了php实现SAE上使用storage上传与下载文件的方法。分享给大家供大家参考。具体如下: <?php if ($_FILES["file"]["erro...

php异常处理捕获错误整理

PHP的错误报告有三种: 1、错误,语法解析错误,致命错误 2、警告 3、注意 后果: 错误 -> 致命错误,会终止已下程序的执行,语法错误的话,PHP压根就没执行。 警告 -&g...