Home > php教程 > php手册 > PHP中数组合并的方法

PHP中数组合并的方法

WBOY
Release: 2016-06-13 09:17:55
Original
1125 people have browsed it

PHP中数组合并的方法

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

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

Code:

$arr1 = array(
    'astr1' => 'astr1',
    3 => 'anum1'
);

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

print_r(array_merge($arr1, $arr2));
Copy after login

Result:

Array
(
    [astr1] => bstr2
    [0] => anum1
    [1] => bstr4
    [2] => bnum2
    [bstr1] => bstr1
    [3] => bnum3
    [4] => bstr3
)
Copy after login

Conclusion:

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

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

当key为‘0’时,按照数字处理;当key为'1',‘2’等其他数字字符串时,会覆盖前面key为1,2的值

Related labels:
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