In PHP, the array_merge_recursive function is used to combine cells of one or more arrays, and the values in an array are appended to the previous array, returning as an array of results. This article will detail the use of array_merge_recursive functions and examples.
Let's first introduce the basic syntax of the array_merge_recursive function:
Array array_merge_recursive (array $array 1 [, Array $ ...])
Array_merge_recursive () Merges the cells of one or more arrays, and the values in one array are appended to the previous array. Returns an array as the result.
If the input array has the same string key name, the values are merged into an array, which is recursive, so if a value itself is an array, this function merges it into another array according to the corresponding entry. However, if the array has the same array key name, the latter value will not overwrite the original value, but append to the back.
Parameter introduction:
Parameters |
Description |
Array1 |
Necessary. The initial array to merge. |
Array2 |
Optional. A list of array variables for recursive merging. |
return value
An array of results in which the values are merged from the attached parameters.
Attention:
The difference between this function and the Array_merge () function is to handle two or more array elements with the same key name. Array_merge_recursive () does not overwrite the key name, but instead recursively composes the values of multiple identical key names into an array.
If you enter only an array to the array_merge_recursive () function, and the result is the same as Array_merge (), the function returns a new array with the integer key name, whose key name is indexed in 0.
Instance:
<?php
$ar 1 = Array ("COLOR" => Array ("Favorite" => "Red"), 5);
$ar 2 = Array ("COLOR" => Array ("Favorite" => "green", "Blue"));
$result = array_merge_recursive ($ar 1, $ar 2);
Print_r ($result);
? >
Results:
Array
(
[color] => Array ([
favorite] => array
([
0] => red
[1] => green< c15/>)
[0] => Blue
)
[0] => 5
[1] =>
Thank you for reading, I hope to help you, thank you for your support for this site!