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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-08 17:24:13
Original
1469 people have browsed it

在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);
?>

Related labels:
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 Issues
How to create an array within an array?
From 1970-01-01 08:00:00
0
0
0
php array
From 1970-01-01 08:00:00
0
0
0
Array to array
From 1970-01-01 08:00:00
0
0
0
php array rotation
From 1970-01-01 08:00:00
0
0
0
array
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template