1. 程式人生 > >php數組合並array_merge()函式

php數組合並array_merge()函式

<?php

// 把兩個數組合併為一個數組:

$a1=array("red","green");
$a2=array("blue","yellow");
$a3 = array_merge($a1,$a2);
print_r($a3);

結果:Array ( [0] => red [1] => green [2] => blue [3] => yellow ) 

?>
<?php

// 將兩個關聯數組合併為一個數組:

$a1=array("a"=>"red","b"=>"green");
$a2=array("c"=>"blue","b"=>"yellow");
print_r(array_merge($a1,$a2));

結果:Array ( [a] => red [b] => yellow [c] => blue ) 

?>
<?php

// 僅使用一個帶有整數鍵名的陣列引數:

$a=array(3=>"red",4=>"green");
print_r(array_merge($a));

結果:Array ( [0] => red [1] => green ) 

?>