php array merge recursive
array_merge
(PHP 4, PHP 5, PHP 7, PHP 8)
array_merge — Сливает один или большее количество массивов
Описание
Сливает элементы одного или большего количества массивов таким образом, что значения одного массива присоединяются к концу предыдущего. Результатом работы функции является новый массив.
Если входные массивы имеют одинаковые строковые ключи, тогда каждое последующее значение будет заменять предыдущее. Однако, если массивы имеют одинаковые числовые ключи, значение, упомянутое последним, не заменит исходное значение, а будет добавлено в конец массива.
В результирующем массиве значения исходного массива с числовыми ключами будут перенумерованы в возрастающем порядке, начиная с нуля.
Список параметров
Возвращаемые значения
Возвращает результирующий массив. Если вызывается без аргументов, возвращает пустой массив ( array ).
Список изменений
Версия | Описание |
---|---|
7.4.0 | Функция теперь может быть вызвана без каких-либо параметров. Ранее требовался хотя бы один параметр. |
Примеры
Пример #1 Пример использования array_merge()
Результат выполнения данного примера:
Пример #2 Простой пример использования array_merge()
Помните, что числовые ключи будут перенумерованы!
Если вы хотите дополнить первый массив элементами второго без перезаписи элементов первого массива и без переиндексации, используйте оператор объединения массивов + :
Ключи из первого массива будут сохранены. Если ключ массива существует в обоих массивах, то будет использован элемент из первого массива, а соответствующий элемент из второго массива будет проигнорирован.
Пример #3 Пример использования array_merge() с не массивами
Результат выполнения данного примера:
Смотрите также
User Contributed Notes 2 notes
In some situations, the union operator ( + ) might be more useful to you than array_merge. The array_merge function does not preserve numeric key values. If you need to preserve the numeric keys, then using + will do that.
[ 0 ] = «zero» ;
$array1 [ 1 ] = «one» ;
$array2 [ 1 ] = «one» ;
$array2 [ 2 ] = «two» ;
$array2 [ 3 ] = «three» ;
//This will result in::
?>
Note the implicit «array_unique» that gets applied as well. In some situations where your numeric keys matter, this behaviour could be useful, and better than array_merge.
array_merge_recursive
(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)
array_merge_recursive — Рекурсивное слияние одного или более массивов
Описание
Функция array_merge_recursive() сливает элементы двух или более массивов таким образом, что значения одного массива присоединяются в конец другого. Возвращает результирующий массив.
Если входные массивы имеют одинаковые строковые ключи, то значения этих ключей сливаются в массив, и это делается рекурсивно, так что если одно из значений является массивом, то функция сливает его с соответствующим значением в другом массиве. Однако, если массивы имеют одинаковые числовые ключи, каждое последующее значение не заменит исходное значение, а будет добавлено в конец массива.
Список параметров
Рекурсивно сливаемые массивы.
Возвращаемые значения
Список изменений
Версия | Описание |
---|---|
7.4.0 | Функция теперь может быть вызвана без каких-либо параметров. Ранее требовался хотя бы один параметр. |
Примеры
Пример #1 Пример использования array_merge_recursive()
Результат выполнения данного примера:
Смотрите также
User Contributed Notes 37 notes
I refactored the Daniel’s function and I got it:
This is a simple, three line approach.
In Addition to felix dot ospald at gmx dot de in my opinion there is no need to compare keys with type-casting, as a key always is changed into an integer if it could be an integer. Just try
$a = array(‘1’=>’1’);
echo gettype(key($a));
It will echo ‘integer’. So for having Integer-Keys simply appended instead of replaced, add the Line:
A Condition I used is:
elseif (is_null($v)) unset($arr[$k]);
And the last one: If you want to use this approach for more than 2 Arrays, simply use this:
I little bit improved daniel’s and gabriel’s contribution to behave more like original array_merge function to append numeric keys instead of overwriting them and added usefull option of specifying which elements to merge as you more often than not need to merge only specific part of array tree, and some parts of array just need to let overwrite previous. By specifying helper element mergeWithParent=true, that section of array will be merged, otherwise latter array part will override former. First level of array behave as classic array_merge.
This is my version of array_merge_recursive without overwriting numeric keys:
function array_merge_recursive_new () <
There are a lot of examples here for recursion that are meant to behave more like array_merge() but they don’t get it quite right or are fairly customised. I think this version is most similar, takes more than 2 arguments and can be renamed in one place:
*Any key only appearing in the right one will be ignored*
— as I didn’t need values appearing only in the right in my implementation, but if you want that you could make some fast fix.
If what you want is merge all values of your array that are arrays themselves to get a resulting array of depth one, then you’re more looking for array_flatten function.
Unfortunately I did not find such native function in php, here is the one I wrote:
Here is a fairly simple function that replaces while recursing.
$array2 = array(
‘liquids’ => array(
‘water’ => ‘hot’
, ‘milk’ => ‘wet’
)
);
Result 2 is:
Array
(
[liquids] => Array
(
[water] => Array
(
[0] => cold
[1] => fizzy
[2] => clean
)
Here’s my function to recursively merge two arrays with overwrites. Nice for merging configurations.
I’ve edit this version even a little bit more, so that the function does not override any values, but inserts them at a free key in the array:
$array1 = array(
100 => array(30),
200 => array(20, 30)
);
$array2 = array(
100 => array(40),
201 => array(60, 30)
);
Output with array_merge_recursive:
Array
(
[0] => Array
(
[0] => 30
)
)
This is not the result, I expect from a MERGE-Routine.
Output with the current function:
This is what I want 🙂
In this version the values are overwritten only if they are not an array. If the value is an array, its elements will be merged/overwritten:
// We walk through each arrays and put value in the results (without
// considering previous value).
$result = array();
An alternative solution where this function does not produce the desired output: Pass a custom recursive function to array_reduce():
For example (Using PHP 7 capabilities to create recursive anonymous function):
Sharing my code to reserve the numeric keys:
$merged = array_merge_recursive($merged, revise_keys($candidate));
>
walfs version is pretty good, but it always assumes we want numeric keys as numeric keys. There are possibilities where a numeric key is actually a string ‘123’
For that I modified the function so that the last argument is a true switch to turn keys can be numeric on. Default is that keys are all string.
Attention.
(used PHP 5.4. XAMPP)
Values with numeric keys are always appended. The index of the merge array is determined by the startindex of the first array. Value4s with numeric keys seems always appended.
If a key looks like an integer, array_merge_recursive will interpret the string as an number.
This recursive array merge function doesn’t renumber integer keys and appends new values to existing ones OR adds a new php array merge recursive pair if the pair doesn’t exist.
Merging arrays recursively some problem about existing keys, so that i wrote the function above like this:
This recursive array merge function doesn’t renumber integer keys and appends new values to existing ones OR adds a new php array merge recursive pair if the pair doesn’t exist.
Proper simplistic example:
Sometimes you need to modify an array with another one here is my approach to replace an array’s content recursively with delete opiton. Here i used «::delete::» as reserved word to delete items.
/**
* Merges any number of arrays of any dimensions, the later overwriting
* previous keys, unless the key is numeric, in whitch case, duplicated
* values will not be added.
*
* The arrays to be merged are passed as arguments to the function.
*
* @access public
* @return array Resulting array, once all have been merged
*/
function array_merge_replace_recursive () <
// Holds all the arrays passed
$params = & func_get_args ();
I’ve tried these array_merge_recursive functions without much success. Maybe it’s just me but they don’t seem to actually go more than one level deep? As with all things, its usually easier to write your own, which I did and it seems to work just the way I wanted. Anyways, my function hasn’t been tested extensively, but it’s a simple function, so in hopes that this might be useful to someone else I’m sharing.
Also, the PHP function array_merge_recursive() didn’t work for my purposes because it didn’t overwrite the values like I needed it to. You know how it works, it just turns it into an array with multiple values. not helpful if your code is expecting one string.
// STRATEGY
/*
Merge array1 and array2, overwriting 1st array values with 2nd array
values where they overlap. Use array1 as the base array and then add
in values from array2 as they exist.
Walk through each value in array2 and see if a value corresponds
in array1. If it does, overwrite with second array value. If it’s an
array, recursively execute this function and return the value. If it’s
a string, overwrite the value from array1 with the value from array2.
If a value exists in array2 that is not found in array1, add it to array1.
*/
This behavior also occurs if the value is the empty array.
In fact, in the above example, interchanging the empty array with
any and all occurences of NULL will yield the same result.
var_dump ( array_merge_recursive2 (array( ‘A’ => array( ‘A’ => 2 )), array( ‘A’ => array( ‘A’ => null ))));
/*
array(1) < ["A"]=>array(1) < ["A"]=>NULL > >
*/
This function tends to reindex arrays, which is not mentioned in the function description.
I just tried to run that function on a three dimensional array, containing errormessages.
The first dim. contains the severity of the error (‘warn’, ‘crit’) the second dim the linenumber (numerical) and the third one consists of errormessages
# Array printout:
Array
(
[ warn ] => Array // severity (associative)
(
[ 2 ] => Array // linenumber (numerical)
(
[ 0 ] => «Category does not exist»
[ 1 ] => «Manufacturer does not exist»
)
)
)
?>
If i now merge two or more of those arrays using array_merge_recursive(), the linenumbers are not conserved. Instead of, they are all renumbered, starting with 0.
Just thought anyone may want to know about that. 🙂
regards, smilingrasta
I ran into a fairly unique situation where array_merge_recursive ALMOST did what I wanted, but NOT QUITE. I read through all of the comments, and I didn’t find anything that really helped me. I saw a lot of functions submitted that were just trying to recreate array_replace_recursive. This is not that.
Take a look at the code and try it out. Hopefully it helps someone in need!
/** We can skip the rest of the loop */
continue;
>
php, array_merge_recursive works well with string keys only
so lets take a glance: the only part is the ‘b’ keys, they are actually works. I dont want to overwrite anything of it but putting them together to an array. Thats good! But then keys the other numeric keys (int or string) are screwed up. I want to have this as result:
2 Answers 2
Let’s break down this question into to separate problems:
To be honest, I don’t know an easy way of solving this. I’m not sure there is one. And even if, I’m not sure you really want it. Such a function will lead to arrays having values that are a string or an array. How would you handle such an array?
Using string keys would help you out in this case, but after learning more about array_merge_recursive I decided to avoid this function when possible. After I asked this question someone filed it as a bug in it since PHP 7.0, since it works differently in PHP 5.x.
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
To make it worse, it handles differently when handling the nested arrays:
So effectively, array_merge_resursive isn’t really resursive.
Please note that PHP is very consistent in being inconsistent. While array_merge_recursive isn’t recursive, array_replace_recursive doesn’t replace (it also appends):
If the key exists in the second array, and not the first, it will be created in the first array.
In many cases this is desired behavior and since naming functions isn’t PHP’s strongest point, you can consider this as a minor issue.
Why is array_merge_recursive not recursive?
I expected to get an array with 3 elements: keys 1, 2, and 3. However, the function returns an array with keys 0, 1, 2 and 3. So 4 elements, while I expected only 3. When I replace the numbers by their alphabetical equivalents (a, b, c) it returns an array with only 3 elements: a, b and c.
This is (to me at least) unexpected behaviour, but at least it’s documented:
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
Question 1: recursive or not?
But that’s not were this question ends. I thought array_*_resursive would be a recursive function:
Recursion is a kind of function call in which a function calls itself. Such functions are also called recursive functions. Structural recursion is a method of problem solving where the solution to a problem depends on solutions to smaller instances of the same problem.
This is expected output, because both arrays have a numeric key ( 1 ), so the second will be appended to the first. The new array starts with key 0. But let’s get back to the very first example:
Question 2: String or integer key makes a big difference.
While writing this question, I found something else. It’s getting more confusing when replacing the numeric key with a string key in a sub-array:
array_merge_recursive
(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)
array_merge_recursive — Merge one or more arrays recursively
Description
array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.
If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.
Parameters
Variable list of arrays to recursively merge.
Return Values
Changelog
Version | Description |
---|---|
7.4.0 | This function can now be called without any parameter. Formerly, at least one parameter has been required. |
Examples
Example #1 array_merge_recursive() example
The above example will output:
See Also
User Contributed Notes 37 notes
I refactored the Daniel’s function and I got it:
This is a simple, three line approach.
In Addition to felix dot ospald at gmx dot de in my opinion there is no need to compare keys with type-casting, as a key always is changed into an integer if it could be an integer. Just try
$a = array(‘1’=>’1’);
echo gettype(key($a));
It will echo ‘integer’. So for having Integer-Keys simply appended instead of replaced, add the Line:
A Condition I used is:
elseif (is_null($v)) unset($arr[$k]);
And the last one: If you want to use this approach for more than 2 Arrays, simply use this:
I little bit improved daniel’s and gabriel’s contribution to behave more like original array_merge function to append numeric keys instead of overwriting them and added usefull option of specifying which elements to merge as you more often than not need to merge only specific part of array tree, and some parts of array just need to let overwrite previous. By specifying helper element mergeWithParent=true, that section of array will be merged, otherwise latter array part will override former. First level of array behave as classic array_merge.
This is my version of array_merge_recursive without overwriting numeric keys:
function array_merge_recursive_new () <
There are a lot of examples here for recursion that are meant to behave more like array_merge() but they don’t get it quite right or are fairly customised. I think this version is most similar, takes more than 2 arguments and can be renamed in one place:
*Any key only appearing in the right one will be ignored*
— as I didn’t need values appearing only in the right in my implementation, but if you want that you could make some fast fix.
If what you want is merge all values of your array that are arrays themselves to get a resulting array of depth one, then you’re more looking for array_flatten function.
Unfortunately I did not find such native function in php, here is the one I wrote:
Here is a fairly simple function that replaces while recursing.
$array2 = array(
‘liquids’ => array(
‘water’ => ‘hot’
, ‘milk’ => ‘wet’
)
);
Result 2 is:
Array
(
[liquids] => Array
(
[water] => Array
(
[0] => cold
[1] => fizzy
[2] => clean
)
Here’s my function to recursively merge two arrays with overwrites. Nice for merging configurations.
I’ve edit this version even a little bit more, so that the function does not override any values, but inserts them at a free key in the array:
$array1 = array(
100 => array(30),
200 => array(20, 30)
);
$array2 = array(
100 => array(40),
201 => array(60, 30)
);
Output with array_merge_recursive:
Array
(
[0] => Array
(
[0] => 30
)
)
This is not the result, I expect from a MERGE-Routine.
Output with the current function:
This is what I want 🙂
In this version the values are overwritten only if they are not an array. If the value is an array, its elements will be merged/overwritten:
// We walk through each arrays and put value in the results (without
// considering previous value).
$result = array();
An alternative solution where this function does not produce the desired output: Pass a custom recursive function to array_reduce():
For example (Using PHP 7 capabilities to create recursive anonymous function):
Sharing my code to reserve the numeric keys:
$merged = array_merge_recursive($merged, revise_keys($candidate));
>
walfs version is pretty good, but it always assumes we want numeric keys as numeric keys. There are possibilities where a numeric key is actually a string ‘123’
For that I modified the function so that the last argument is a true switch to turn keys can be numeric on. Default is that keys are all string.
Attention.
(used PHP 5.4. XAMPP)
Values with numeric keys are always appended. The index of the merge array is determined by the startindex of the first array. Value4s with numeric keys seems always appended.
If a key looks like an integer, array_merge_recursive will interpret the string as an number.
This recursive array merge function doesn’t renumber integer keys and appends new values to existing ones OR adds a new php array merge recursive pair if the pair doesn’t exist.
Merging arrays recursively some problem about existing keys, so that i wrote the function above like this:
This recursive array merge function doesn’t renumber integer keys and appends new values to existing ones OR adds a new php array merge recursive pair if the pair doesn’t exist.
Proper simplistic example:
Sometimes you need to modify an array with another one here is my approach to replace an array’s content recursively with delete opiton. Here i used «::delete::» as reserved word to delete items.
/**
* Merges any number of arrays of any dimensions, the later overwriting
* previous keys, unless the key is numeric, in whitch case, duplicated
* values will not be added.
*
* The arrays to be merged are passed as arguments to the function.
*
* @access public
* @return array Resulting array, once all have been merged
*/
function array_merge_replace_recursive () <
// Holds all the arrays passed
$params = & func_get_args ();
I’ve tried these array_merge_recursive functions without much success. Maybe it’s just me but they don’t seem to actually go more than one level deep? As with all things, its usually easier to write your own, which I did and it seems to work just the way I wanted. Anyways, my function hasn’t been tested extensively, but it’s a simple function, so in hopes that this might be useful to someone else I’m sharing.
Also, the PHP function array_merge_recursive() didn’t work for my purposes because it didn’t overwrite the values like I needed it to. You know how it works, it just turns it into an array with multiple values. not helpful if your code is expecting one string.
// STRATEGY
/*
Merge array1 and array2, overwriting 1st array values with 2nd array
values where they overlap. Use array1 as the base array and then add
in values from array2 as they exist.
Walk through each value in array2 and see if a value corresponds
in array1. If it does, overwrite with second array value. If it’s an
array, recursively execute this function and return the value. If it’s
a string, overwrite the value from array1 with the value from array2.
If a value exists in array2 that is not found in array1, add it to array1.
*/
This behavior also occurs if the value is the empty array.
In fact, in the above example, interchanging the empty array with
any and all occurences of NULL will yield the same result.
var_dump ( array_merge_recursive2 (array( ‘A’ => array( ‘A’ => 2 )), array( ‘A’ => array( ‘A’ => null ))));
/*
array(1) < ["A"]=>array(1) < ["A"]=>NULL > >
*/
This function tends to reindex arrays, which is not mentioned in the function description.
I just tried to run that function on a three dimensional array, containing errormessages.
The first dim. contains the severity of the error (‘warn’, ‘crit’) the second dim the linenumber (numerical) and the third one consists of errormessages
# Array printout:
Array
(
[ warn ] => Array // severity (associative)
(
[ 2 ] => Array // linenumber (numerical)
(
[ 0 ] => «Category does not exist»
[ 1 ] => «Manufacturer does not exist»
)
)
)
?>
If i now merge two or more of those arrays using array_merge_recursive(), the linenumbers are not conserved. Instead of, they are all renumbered, starting with 0.
Just thought anyone may want to know about that. 🙂
regards, smilingrasta
I ran into a fairly unique situation where array_merge_recursive ALMOST did what I wanted, but NOT QUITE. I read through all of the comments, and I didn’t find anything that really helped me. I saw a lot of functions submitted that were just trying to recreate array_replace_recursive. This is not that.
Take a look at the code and try it out. Hopefully it helps someone in need!
/** We can skip the rest of the loop */
continue;
>