/** - desc:数组合并
- link:bbs.it-home.org
- date:2013/2/22
- */
- $a = array( 'a' => 1, 'b' => 2 );
- $b = array( 'b' => 3, 'd' => 4 );
//数组合并 - $array_merge = array_merge( $a, $b );
- echo '
'; - print_r( $array_merge );
//数组递归合并 - $array_merge_recursive = array_merge_recursive( $a, $b );
- print_r( $array_merge_recursive );
- echo '';
- ?>
输出结果:
Array
(
[a] => 1
=> 3
[d] => 4
)
Array
(
[a] => 1
=> Array
(
[0] => 2
[1] => 3
)
[d] => 4
)
我们可以看出:
array_merge遇到同值的情况是第二个数组key覆盖第一个数组key,而array_merge_recursive函数则对同值数组采取了下标合并。 |