Home > Backend Development > PHP Tutorial > PHP中怎么合并几个数组成为一个数组

PHP中怎么合并几个数组成为一个数组

WBOY
Release: 2016-06-13 12:26:07
Original
1780 people have browsed it

PHP中如何合并几个数组成为一个数组

<span style="color: #000000;">一维数组的合并</span>
Copy after login
<?php  $arr1=array("a","b","c");  $arr2=array("c","d","e");  $myarray=array_merge($arr1,$arr2);  print_r($myarray);  $myarray=array_unique($myarray);  print_r($myarray);?> 
Copy after login

<span style="color: #000000;"> 二维数组的合并 PHP 中的 array_merge_recursive() 函数可以实现 将一个或多个数组的元素的合并起来,一个数组中的值附加在前一个数组的后面。并返回作为结果的数组。 当有重复的键名时,值不会被覆盖,而是将多个相同键名的值递归组成一个数组。</span>
Copy after login
array_merge_recursive(array1,array2,array3...)
Copy after login

Copy after login
<span style="color: #000000;">参数说明 array1 必需。输入的第一个数组。 array2 必需。输入的第二个数组。 array3 可选。可指定的多个输入数组。 例如:</span>
Copy after login
<?php  $a1=array("a"=>"Horse","b"=>"Dog");  $a2=array("c"=>"Cow","b"=>"Cat");  print_r(array_merge_recursive($a1,$a2));?> 
Copy after login

<span style="color: #000000;"><br /> 输出: <br /></span>
Copy after login
Array (   [a] => Horse   [b] => Array (     [0] => Dog     [1] => Cat )   [c] => Cow ) 
Copy after login

<span style="color: #000000;">注:array_merge() 函数也可以实现,与 array_merge_recursive()函数不同是,如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的)。如果数组是数字索引的,则键名会以连续方式重新索引。</span>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template