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 microtime获取浮点的时间戳

一直以来用这个函数获取: 复制代码 代码如下: function microtime_float(){ list($usec, $sec) = explode(" ", microtim...

php解决和避免form表单重复提交的几种方法

前言 为什么要避免form表单被重复提交呢?因为我们不想让我们的服务器重复处理没必要的数据,同时我们也是避免我们的数据库产生重复的数据,避免表单重复提交也是让我们的网站更安全的一种表现。...

php pki加密技术(openssl)详解

复制代码 代码如下:<?php//pki加密//使用pki加密需要开启 openssl扩展//php.ini extension = php_openssl.dll扩展/*pki模...

ueditor 1.2.6 使用方法说明

ueditor 1.2.6 使用方法说明

本文以php版本为例:文件下载:http://ueditor.baidu.com/website/download.html还可以自己先定义内容,然后下载,这样可以帮助我们精简不少东西。...

PHP添加Xdebug扩展的方法

xdegug是一个很好的php调试扩展,安装方法也很简单,基本和其他的扩展安装方式差不多. 一、下载对应的DLL 下载地址:https://xdebug.org/download.php...