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中。
我错了。

相关文章

redis查看连接数及php模拟并发创建redis连接的方法

max_redis.php <?php set_time_limit (0); for($i=1;$i<=1050;$i++){ exec("nohup p...

PHP数组生成XML格式数据的封装类实例

本文实例讲述了PHP数组生成XML格式数据的封装类。分享给大家供大家参考,具体如下: 类库代码:MakeXML.php: <?php /** * MakeXML * *...

PHP中函数内引用全局变量的方法

先看下面的代码: 复制代码 代码如下:<?php $var1 = "#####"; $var2 = "&&&&&"; function global_references($use...

php防止CC攻击代码 php防止网页频繁刷新

网页快速恶意刷新,cc攻击就是攻击者利用代理服务器生成指向目标站点的合法请求,模拟多用户不停的对受害网站进行访问,特别是访问那些需要大量数据操作需要大量CUP时间的页面,最终导致目标网站...

php 安全过滤函数代码

复制代码 代码如下: //安全过滤输入[jb] function check_str($string, $isurl = false) { $string = preg_repl...