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实现C#山寨ArrayList的方法

本文实例讲述了PHP实现C#山寨ArrayList的方法。分享给大家供大家参考。具体如下: class ArrayList { public $length; public $n...

PHP中::、-&amp;gt;、self、$this几种操作符的区别介绍

在访问PHP类中的成员变量或方法时,如果被引用的变量或者方法被声明成const(定义常量)或者static(声明静态),那么就必须使用操作符::,反之如果被引用的变量或者方法没有被声明成...

PHP实现的登录,注册及密码修改功能分析

PHP实现的登录,注册及密码修改功能分析

本文实例讲述了PHP实现登录,注册及密码修改功能的方法。分享给大家供大家参考,具体如下: 这里介绍注册,登录,修改密码的界面布局与功能实现: 1.登录 2.忘记密码 3.免费注册...

详解PHP数组赋值方法

PHP数组还是比较常用的,于是我研究了一下PHP数组赋值,在这里拿出来和大家分享一下,希望对大家有用。 所谓数组就是一组变量的集合保存在计算机的内存中,这些变量可以是不同的类型,包括整数...

据说是雅虎的一份PHP面试题附答案

从网上搜集到的,据说是雅虎的面试题。 1. Which of the following will not add john to the users array? 1. $users[...