Home > php教程 > php手册 > Commonly used array functions in PHP (7) Array merge array_merge() and array_merge_recursive()

Commonly used array functions in PHP (7) Array merge array_merge() and array_merge_recursive()

WBOY
Release: 2016-10-28 15:03:29
Original
2079 people have browsed it
<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>
Copy after login

Both functions are used to merge arrays. Parameters can be arrays of 1 to n. (Uh, I don’t understand what is used when the parameter is an array. Who knows? Tell me.)


Output result:

Line 3:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[color] => blue //Please note the difference in this line
[4] => a
[5] => b
[6] => c
)

Line 4:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[color] => Array //Pay attention to the difference here
(
[0] => red
[1] => blue
)

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

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template