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实现的AES双向加密解密功能示例【128位】

本文实例讲述了PHP实现的AES双向加密解密功能。分享给大家供大家参考,具体如下: <?php /* * Created on 2018-9-1 * * To c...

php中socket通信机制实例详解

本文实例讲述了php中socket通信机制及用法。分享给大家供大家参考。具体分析如下: 一、socket是什么 什么是socket 所谓socket通常也称作"套接字",用于描述ip地址...

phpword插件导出word文件时中文乱码问题处理方案

最近一个项目开发要用到PHP技术导出Word文档,比较了几种方案,首先是使用Microsoft Office自带的ActiveX/COM组件,比如Word.Application,这种方...

php模块memcache和memcached区别分析

1.目前大多数php环境里使用的都是不带d的memcache版本,这个版本出的比较早,是一个原生版本,完全在php框架内开发的。与之对应的带d的memcached是建立在libmemca...

基于PHP CURL用法的深入分析

如下所示:复制代码 代码如下:<?phpheader('Context-Type:text/html;charset:gb2312;');$urls = array( '...