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

相关文章

简单实用的PHP防注入类实例

本文实例讲述了简单实用的PHP防注入类。分享给大家供大家参考。具体如下: PHP防注入注意要过滤的信息基本是get,post,然后对于sql就是我们常用的查询,插入等等sql命令了,下面...

PHP命名空间简单用法示例

本文实例讲述了PHP命名空间简单用法。分享给大家供大家参考,具体如下: 有三个文件,分别是space1.php,space2.php,use.php吧,在同一目录里。 space1.ph...

PHP 万年历实现代码

PHP 万年历实现代码

使用PHP实现万年历功能的要点: •得到当前要处理的月份总共有多少天$days •得到当前要处理的月份的一号是星期几$dayofweek $days的作用:知道要...

php表单提交与$_POST实例分析

php表单提交与$_POST实例分析

本文实例分析了php的表单提交与$_POST。分享给大家供大家参考。具体如下: 这里要注意:表单 checkbox 的 name 需要以数组形式来命名。 表单文件:index.php 复...

PHP用GD库生成高质量的缩略图片

以下是PHP源代码(ResizeImage.php)。 复制代码 代码如下: <?php $FILENAME="image.thumb"; // 生成图片的宽度 $RESIZEWI...