首頁  >  文章  >  php教程  >  php中的常用陣列函數(七) 陣列合併 array_merge()和array_merge_recursive()

php中的常用陣列函數(七) 陣列合併 array_merge()和array_merge_recursive()

WBOY
WBOY原創
2016-10-28 15:03:292022瀏覽
<span style="color: #008080">1</span> <span style="color: #800080">$arr1</span> = <span style="color: #0000ff">array</span>(1, 2, 3, 4, 'color'=>'red'<span style="color: #000000">);
</span><span style="color: #008080">2</span> <span style="color: #800080">$arr2</span> = <span style="color: #0000ff">array</span>('a', 'b', 'c', 'color'=>'blue'<span style="color: #000000">);
</span><span style="color: #008080">3</span> <span style="color: #008080">print_r</span>(<span style="color: #008080">array_merge</span>(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$arr2</span>));<span style="color: #008000">//同名索引的值会覆盖</span>
<span style="color: #008080">4</span> <span style="color: #008080">print_r</span>(<span style="color: #008080">array_merge_recursive</span>(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$arr2</span>));<span style="color: #008000">//</span><span style="color: #008000">相同的键名 不会覆盖,如果是单个元素会在转为一个一维数组</span>

兩個函數作用都是合併數組。參數可以是1到n個陣列。 (呃,不懂參數是1個陣列的時候啥用的。誰知道告訴我。)


 

輸出結果:

第3行:

Array
(
[0] => 1
[1] => 2
[2] => 3> > blue
//這一行有區別注意哦
[4] => a [5] => b
[6] =>> c)
))
第4行:
Array

(

[0] => 1
[1] => 2
[2] => 3> > Array
//注意這個地方有區別
(
[0] => red
[1] => blue [1] => blue
[4] => a
[5] => b
[6] => c
)

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn