- 相關推薦
PHP合并兩個數組的兩種方式的異同
對數組的array_merge函數和+運算符比較迷惑,寫了個小程序比較下發(fā)現(xiàn)了他們的不同,就跟隨百分網小編一起去了解下吧,想了解更多相關信息請持續(xù)關注我們應屆畢業(yè)生考試網!
特別是+運算符,他的意思是,將右邊的數組單元(去重復)追加到左邊數組的后面。
復制代碼 代碼如下:
<?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
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 4
[4] => 5
[5] => 6
)
第二種情況
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => a
[7] => x
[8] => y
)
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
Array
(
[0] => a
[1] => x
[2] => y
[3] => d
[4] => e
[5] => f
)
第三種情況
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
[6] => a
[7] => x
[8] => y
)
Array
(
[1] => a
[2] => b
[3] => c
[4] => d
[5] => e
[6] => f
[7] => x
[8] => y
)
Array
(
[1] => a
[7] => x
[8] => y
[2] => b
[3] => c
[4] => d
[5] => e
[6] => f
)
【PHP合并兩個數組的兩種方式的異同】相關文章:
php合并數組中相同元素的方法07-17
PHP兩個數組相加的方法07-14
PHP數組的排序09-28
PHP數組基本介紹09-06
PHP數組函數知識10-24
PHP一般數組與對象數組11-13
PHP技巧:數組交集的優(yōu)化09-29
php數組長度的方法10-05
PHP新手如何學習數組10-15