Home  >  Article  >  Backend Development  >  Introduction to how to use the PHP array_merge_recursive() function

Introduction to how to use the PHP array_merge_recursive() function

WBOY
WBOYOriginal
2023-06-27 12:21:521249browse

The array_merge_recursive() function in PHP is a method of merging two or more arrays into one array. The difference between it and the array_merge() function is that it not only merges array elements, but also merges each array. The elements of each array are recursively combined into a larger array. Therefore, the array_merge_recursive() function is very useful when deeply nested and need to merge arrays recursively. This article will introduce the specific use of the array_merge_recursive() function.

The basic syntax of the array_merge_recursive() function is as follows:

array array_merge_recursive(array $array1 [, array $array2 [, array $... ]] )

Among them, $array1 is a required parameter and represents the first array to be merged. $array2 to $arrayN are optional parameters indicating other arrays to be merged.

Example 1:

The following is a simple example showing how to merge two arrays into one:

$array1 = array("color" => array("favorite" => "red"), 5);
$array2 = array(10, "color" => array("favorite" => "green", "blue"));

$result = array_merge_recursive($array1, $array2);
print_r($result);

The output is as follows:

Array
(
    [color] => Array
        (
            [favorite] => Array
                (
                    [0] => red
                    [1] => green
                )
            [0] => blue
        )
    [0] => 5
    [1] => 10
)

You can see that the array elements are successfully merged into an array, and the same array key name will be saved.

Example 2:

The following example will show how to add other arrays based on an array. Specifically, we first create an array named "people" and then merge three arrays named "John", "Sarah" and "Michael" into this array:

$people = array(
    "John" => array("age" => 25, "sex" => "male"),
    "Sarah" => array("age" => 31, "sex" => "female"),
);

$people = array_merge_recursive($people, array(
    "John" => array("height" => "6'1""),
    "Michael" => array("age" => 37)
));

print_r($people);

Output results As follows:

Array
(
    [John] => Array
        (
            [age] => Array
                (
                    [0] => 25
                )
            [sex] => Array
                (
                    [0] => male
                )
            [height] => Array
                (
                    [0] => 6'1"
                )
        )
    [Sarah] => Array
        (
            [age] => Array
                (
                    [0] => 31
                )
            [sex] => Array
                (
                    [0] => female
                )
        )
    [Michael] => Array
        (
            [age] => Array
                (
                    [0] => 37
                )
        )
)

You can see that the "age" and "sex" key names of "John" already exist, a "height" key name was added during the merge, and the "Michael" array was completely added to in the "people" array. In other words, the array_merge_recursive() function can completely merge two different arrays into a larger array. Values ​​with the same key name will be recursively merged, and values ​​with different key names will be added to the new array.

Summary:

The array_merge_recursive() function can be used very conveniently when multiple arrays need to be merged end to end. When there are nested subarrays in an array, this function can recursively merge these arrays and avoid key name conflicts in the subarrays.

The above is the detailed content of Introduction to how to use the PHP array_merge_recursive() function. For more information, please follow other related articles on the PHP Chinese website!

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