PHP array 的加法操作代码

yipeiwu_com5年前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插入中文到sqlserver 2008里出现乱码的解决办法分享

今天使用php操作数据库时发现插入SQL Server 2008数据库里的中文字段出现乱码,下面是我一开始时的一些情况: 开发环境是php5.3.3+Apache2.2.17+SQL S...

php中eval函数的危害与正确禁用方法

php的eval函数并不是系统组件函数,因此我们在php.ini中使用disable_functions是无法禁止它的。 但是eval()对于php安全来说具有很大的杀伤力,因此一般不用...

PHPMailer的主要功能特点和简单使用说明

支持邮件 s/mime加密的数字签名 支持邮件多个 TOs, CCs, BCCs and REPLY-TOs 可以工作在任何服务器平台,所以不用担心WIN平台无法发送邮件的问题的 支持文...

PHP实现深度优先搜索算法(DFS,Depth First Search)详解

PHP实现深度优先搜索算法(DFS,Depth First Search)详解

本文实例讲述了PHP实现深度优先搜索算法。分享给大家供大家参考,具体如下: 深度优先搜索的实现原理: 实现代码: <?php class Search_Method...

浅谈php(codeigniter)安全性注意事项

1、httponly session一定要用httponly的否则可能被xxs攻击,利用js获取cookie的session_id。 要用框架的ci_session,更长的位数,http...