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 PDO的一些认识小结

1、PDO(PHP Data Object)扩展为PHP定义了一个访问数据库的轻量、持久的接口。实现PDO接口的每一种数据库驱动都能以正则扩展的形式把各自的特色表现出来。 主要:PDO扩...

自动分页的不完整解决方案

测试代码 <form id="form1" name="form1" method="post" action="">  &...

php画图实例

php画图实例

本文实例讲述了php画图的方法。分享给大家供大家参考。具体实现方法如下: 复制代码 代码如下:<?php  // by MoreWindows $imgWid...

php查询mssql出现乱码的解决方法

本文实例讲述了php查询mssql出现乱码的解决方法。分享给大家供大家参考。具体分析如下: 在php连接mssql时查询出来的全部是乱码,这种问题我根据经验知道是编码问题,下面来给各位总...

PHP计算百度地图两个GPS坐标之间距离的方法

本文实例讲述了PHP计算百度地图两个GPS坐标之间距离的方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:/**  * 计算两个坐标之间的距离(米) &nb...