Home  >  Article  >  Backend Development  >  How to combine arrays in PHP_PHP tutorial

How to combine arrays in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:381381browse

How to combine arrays in PHP

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

Merge the elements of one or more arrays, append the following elements to the previous elements, and return the resulting 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));

Result:

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

Conclusion:

Elements whose key is a numeric value are reordered according to increasing rules starting from key=0. If there are duplicate key values, they are still sorted in increasing order;

Key is the element of the string, arranged in the original order. If there is a duplicate key, the subsequent value will overwrite the previous value;

When the key is ‘0’, it is processed as numbers; when the key is ‘1’, ‘2’ and other numeric strings, the previous values ​​of key 1 and 2 will be overwritten

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/934464.htmlTechArticleMethod of array merging in PHP 1. array array_merge(array $array1[, array $...]) Merge One or more array elements, append the following elements to the previous elements, and return the result...
Statement:
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