本文主要为广大网友提供“PHP合并两个数组的两种方式的异同”,希望对需要PHP合并两个数组的两种方式的异同网友有所帮助,学习一下!
特别是+运算符,他的意思是,将右边的数组单元(去重复)追加到左边数组的后面。
复制代码 代码如下:
<?php
echo "\r\n第一种情况\r\n";
$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 "\r\n第二种情况\r\n";
$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 "\r\n第三种情况\r\n";
$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);
?>
结果如下:
复制代码 代码如下:
第一种情况
Array
(
[0] => 1
=> 2
=> 3
=> 4
=> 5
=> 6
=> 7
=> 8
=> 9
)
Array
(
[0] => 1
=> 2
=> 3
=> 4
=> 5
=> 6
)
Array
(
[0] => 7
=> 8
=> 9
=> 4
=> 5
=> 6
)
第二种情况
Array
(
[0] => a
=> b
=> c
=> d
=> e
=> f
=> a
=> x
=> y
)
Array
(
[0] => a
=> b
=> c
=> d
=> e
=> f
)
Array
(
[0] => a
=> x
=> y
=> d
=> e
=> f
)
第三种情况
Array
(
[0] => a
=> b
=> c
=> d
=> e
=> f
=> a
=> x
=> y
)
Array
(
=> a
=> b
=> c
=> d
=> e
=> f
=> x
=> y
)
Array
(
=> a
=> x
=> y
=> b
=> c
=> d
=> e
=> f
)