在php中合并数组的方法有很多,下面我来介绍最简单快速可实现数组合并的方法,有需要了解的朋友可参考。
<script>ec(2);</script>
数组合并array_merge
例
代码如下 |
复制代码 |
$array1 = array(1=>'0');
$array2 = array(1=> "data");
$result3 = array_merge($array2,$array1);/*结果为$array2和$array1的值,键名被重新分配*/
|
还有一种办法直接使用“+”号
代码如下 |
复制代码 |
a = array(
'a',
);
$b = array(
'u',
);
$c = $a + $b;
var_dump($c);
output:
array(1) {
[0]=>
string(1) "a"
}
|
数组的交集
这部分只介绍一些函数,至于用法是很简单的,不想多说,以免把简单的事情变得复杂了。
追加数组
array array_merge_recursive(array array1,array array2[…,array arrayN])
代码如下 |
复制代码 |
$fruit1 = array("apple" => "red", "banana" => "yellow");
$fruit2 = array("pear" => "yellow", "apple" => "green");
$result = array_merge_recursive($fruit1, $fruit2);
print_r($result);
// output
// Array ( [apple] => Array ( [0] => red [1] => green ) [banana] => yellow [pear] => yellow )
?>
|
关联数组的差集Array_diff()这个不就介绍了
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn