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简单明了函数语法)

1、与mysql相关 mysql_connect 建立一个与MySQL服务器的连接 语法 resource mysql_connect(string server[,string usi...

PHP读取文件,解决中文乱码UTF-8的方法分析

本文实例讲述了PHP读取文件,解决中文乱码UTF-8的方法。分享给大家供大家参考,具体如下: $opts = array( 'file' => array( 'en...

php+jQuery递归调用POST循环请求示例

php+jQuery递归调用POST循环请求示例

本文实例讲述了php+jQuery递归调用POST循环请求的方法。分享给大家供大家参考,具体如下: jQuery 代码部分: $(function(){ _post('demo.p...

phpstorm 正则匹配删除空行、注释行(替换注释行为空行)

phpstorm 正则匹配删除空行、注释行(替换注释行为空行)

使用phpstorm 来编写php 和javascript 代码,感觉还是不错的,用得也很舒服。 遇到了一个需求,有时候在阅读框架源代码的时候 , 想过滤(删除)掉源代码中的注释行,如果...

解析PHP实现多进程并行执行脚本

由于php的进程是不支持多线程的,有些场景为了方便以及提高性能,可以用php实现多进程以弥补这个不足:复制代码 代码如下:#!/usr/bin/env php<?php$cmds=...