Home > Article > Backend Development > php array_merge_recursive array merge
This article mainly introduces the relevant information about php array_merge_recursive merging two or more arrays into one array. Friends who need it can refer to
The array_merge_recursive function in php is used to merge one or more arrays. The cells are combined, the values in one array are appended to the previous array, and the resulting array is returned. This article will introduce in detail the usage and examples of the array_merge_recursive function.
Let’s first introduce the basic syntax of the array_merge_recursive function:
array array_merge_recursive ( array $array1 [, array $... ] )
array_merge_recursive() Merges the cells of one or more arrays, with the values in one array appended to the previous array. Returns the resulting array.
If the input arrays have the same string key name, these values will be merged into an array, which will go on recursively, so if a value itself is an array, this function will follow the corresponding entries into another array. However, if the arrays have the same array key name, the latter value will not overwrite the original value, but will be appended to it.
Parameter introduction:
Parameter | Description |
---|---|
array1 | Required. The initial array to merge. |
array2 | Optional. List of array variables for recursive merging. |
Return Value
A result array whose values are combined from the appended parameters.
Note:
The difference between this function and the array_merge() function is when two or more array elements have the same key name. array_merge_recursive() does not perform key name overwriting, but recursively combines multiple values with the same key name into an array.
If you simply input an array to the array_merge_recursive() function, the result is the same as array_merge(), the function will return a new array with integer keys, with keys starting at 0 for re-indexing.
Example:
<?php $ar1 = array( "color" => array( "favorite" => "red" ), 5 ); $ar2 = array( 10 , "color" => array( "favorite" => "green" , "blue" )); $result = array_merge_recursive ( $ar1 , $ar2 ); print_r ( $result ); ?>
Result:
Array ( [color] => Array ( [favorite] => Array ( [0] => red [1] => green ) [0] => blue ) [0] => 5 [1] => 10 )
The above is the entire content of this article, I hope it will be helpful to everyone’s study.
Related recommendations:
How to set the file size using the readfile() function in php
Analysis of the difference between new self() and new static() in PHP
##PHP implements file lock and process lock
The above is the detailed content of php array_merge_recursive array merge. For more information, please follow other related articles on the PHP Chinese website!