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中文乱码怎么办如何让浏览器自动识别utf-8

刚开始学习php的朋友可能经常遇到问题,就是调试的时候经常显示乱码 每次打开浏览器都要改下编码,很麻烦, 有没有方法让浏览器自动识别utf-8呢? 解决的方法很简单 就是在php里加一句...

PHP读取文件内容后清空文件示例代码

复制代码 代码如下: $fh = fopen($path, "r+"); if( flock($fh, LOCK_EX) ){//加写锁 $old_content=json_decode...

php基于SQLite实现的分页功能示例

本文实例讲述了php基于SQLite实现的分页功能。分享给大家供大家参考,具体如下: 这里操作数据库文件使用的是前面文章《PHP基于PDO实现的SQLite操作类【包含增删改查及事务等操...

PHP中使用CURL获取页面title例子

通过PHP获取页面title内容的实战演示: 范例代码: 复制代码 代码如下: <?php   /*  功能: 取得 URL 页面上的 &...

Ajax请求PHP后台接口返回信息的实例代码

Ajax请求PHP后台接口返回信息的实例代码

前台就是一个表单,这里是用的bootstrop的 <form method="post" > <!-- token验证 --> <!--{{ csr...