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

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

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

一维数组的合并
Copy after login
 
Copy after login

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


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


输出:
Copy after login
Array (   [a] => Horse   [b] => Array (     [0] => Dog     [1] => Cat )   [c] => Cow )
Copy after login

注:array_merge() 函数也可以实现,与 array_merge_recursive()函数不同是,如果键名有重复,该键的键值为最后一个键名对应的值(后面的覆盖前面的)。如果数组是数字索引的,则键名会以连续方式重新索引。
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!