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 CURL模拟登录新浪微博抓取页面内容 基于EaglePHP框架开发

复制代码 代码如下: /** * CURL请求 * @param String $url 请求地址 * @param Array $data 请求数据 */ function curlR...

php的扩展写法总结

为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多。同样的运算代码,使用C来开发,性能会比PHP要提升数百倍。IO操作如CURL,因为耗时主要在IOWait上,C扩展没有明显...

PHP过滤★等特殊符号的正则

复制代码 代码如下: if(preg_match("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",$user)){ ec...

php使用$_POST或$_SESSION[]向js函数传参

在php编程中向js函数传参可以使用$_POST也可使用$_SESSION[' '],也可用echo语句进行输出 复制代码 代码如下: <?php echo "<s...

PHP sleep()函数, usleep()函数

PHP sleep() 函数 定义和用法 sleep() 函数延迟代码执行若干秒。 语法sleep(seconds) seconds 必需。以秒计的暂停时间。 返回值 若成功,返回 0...