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输出指定时间以前时间格式的方法

本文实例讲述了php输出指定时间以前时间格式的方法。分享给大家供大家参考。具体分析如下: 比如说你需要在php中输出3天前,20分钟以前,可以参考下面的代码 function ago...

Drupal 添加模块出现莫名其妙的错误的解决方法(往往出现在模块较多时)

状况如下: 1、点击保按钮后,不能正常转到页面,出现空白页面; 2、刷新页面,发出导航中许多项已经没有了,还存在的项点击也不能进入正常的页面。 测试了多次没有发现任何问题,多次重装、测试...

php获取手机端的号码以及ip地址实例代码

我们在用PHP写移动端程序的时候,有的时候需要直接获取手机号码以及对应的IP地址内容,在此我们给大家整理了详细完整的代码内容,需要的朋友们测试下。 <?php /**...

PHP实现查询手机归属地的方法详解

PHP实现查询手机归属地的方法详解

本文介绍的是PHP实现查询手机归属地的方法,首先来看看手机归属地查询的类图: 后台 MobileQuery类调用: \libs\HttpRquest \libs\ImRedi...

原生JS实现Ajax通过GET方式与PHP进行交互操作示例

原生JS实现Ajax通过GET方式与PHP进行交互操作示例

本文实例讲述了原生JS实现Ajax通过GET方式与PHP进行交互操作。分享给大家供大家参考,具体如下: 一、代码 conn.php <?php $conn=mys...