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实现自动识别原编码并对字符串进行编码转换的方法。分享给大家供大家参考,具体如下: /** * 对数据进行编码转换 * @param array/string...

PHP+Apache实现二级域名之间共享cookie的方法

本文实例讲述了PHP+Apache实现二级域名之间共享cookie的方法。分享给大家供大家参考,具体如下: 简介 login.koastal.com设置domain为koastal.co...

php print EOF实现方法

我写段php代码如下: 复制代码 代码如下:<? if(test case) print<<<EOT <....html code....> EOF;...

PHP的图像处理实例小结【文字水印、图片水印、压缩图像等】

本文实例讲述了PHP的图像处理。分享给大家供大家参考,具体如下:1、添加文字水印//1、打开图片资源   $src="./material/sea.jpg&...

php连接sftp的作用以及实例代码

sftp 协议 使用SSH协议进行FTP传输的协议叫SFTP(安全文件传输)Sftp和Ftp都是文件传输协议。 区别: sftp是ssh内含的协议(ssh是加密的telnet协议),只...