Rumah  >  Artikel  >  php教程  >  php中的常用数组函数(七) 数组合并 array_merge()和array_merge_recursive()

php中的常用数组函数(七) 数组合并 array_merge()和array_merge_recursive()

WBOY
WBOYasal
2016-10-28 15:03:292052semak imbas
<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
[3] => 4
[color] => blue //这一行有区别注意哦
[4] => a
[5] => b
[6] => c
)

第4行:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[color] => Array //注意这个地方有区别
(
[0] => red
[1] => blue
)

[4] => a
[5] => b
[6] => c
)

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn