[PHP] array_merge解析

array_merge(array $array1[, array $...])

参数必须是array,否则PHP会报错。

合并一个或多个数组的元素,将后面的元素追加到前面的元素后面,并返回结果数组。

Code:

 'astr1',
    3 => 'anum1',
    'anone1'
);

$arr2 = array(
    1 => 'bnum1',
    2 => 'bnum2',
    'bstr1' => 'bstr1',
    3 => 'bnum3',
    'astr1' => 'bstr2',
    '0' => 'bstr3',
);

$arr3 = array_merge($arr1, $arr2);
print 'arr3:' . print_r($arr3, true) . PHP_EOL;

$arr4 = array_merge($arr2, $arr1);
print 'arr4:' . print_r($arr4, true) . PHP_EOL;


Result:

PHP version:5.6.2
arr3:Array
(
    [astr1] => bstr2
    [0] => anum1
    [1] => anone1
    [2] => bnum1
    [3] => bnum2
    [bstr1] => bstr1
    [4] => bnum3
    [5] => bstr3
)

arr4:Array
(
    [0] => bnum1
    [1] => bnum2
    [bstr1] => bstr1
    [2] => bnum3
    [astr1] => astr1
    [3] => bstr3
    [4] => anum1
    [5] => anone1
)


Conclusion:

key为数值时的元素,从key=0开始按照递增的规则重新排序,如出现重复的key值时,依旧按递增的顺序排;

key为字符串的元素,按照原来的顺序排列,如出现重复的key时,后面的值会覆盖前面的值;


github:https://github.com/CodeTheWorld/PHP_Language_Exploration


你可能感兴趣的:(PHP)