Home >php教程 >PHP源码 >PHP中合并两个数组array_merge()函数介绍

PHP中合并两个数组array_merge()函数介绍

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:24:131462browse

在php中我们合并数据有两种简单快速的方式一种方式是利用array_merge()函数来操作,另一种办法是利用+号,下面我们来看看哈。

<script>ec(2);</script>

用法

array array_merge ( array $array1 [, array $array2 [, array $... ]] )

例子 1

 代码如下 复制代码
$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge($a1,$a2));
?>

输出:

 代码如下 复制代码

Array ( [a] => Horse [b] => Cat [c] => Cow )

例子2

 代码如下 复制代码

echo "rn第一种情况rn";
$a=array(1,2,3,4,5,6);
$b=array(7,8,9);
 
$c=array_merge ($a,$b);
print_r($c);
$c=$a+$b;
print_r($c);
$c=$b+$a;
print_r($c);
 
 
echo "rn第二种情况rn";
$a=array('a','b','c','d','e','f');
$b=array('a','x','y');
 
$c=array_merge ($a,$b);
print_r($c);
$c=$a+$b;
print_r($c);
$c=$b+$a;
print_r($c);
 
 
echo "rn第三种情况rn";
 
$a=array(
 1=>'a',
 2=>'b',
 3=>'c',
 4=>'d',
 5=>'e',
 6=>'f');
$b=array(
 1=>'a',
 7=>'x',
 8=>'y');
 
$c=array_merge ($a,$b);
print_r($c);
$c=$a+$b;
print_r($c);
$c=$b+$a;
print_r($c);
?>

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