php array merge с сохранением ключей

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.

Источник

PHP: merge two arrays while keeping keys instead of reindexing?

How can I merge two arrays (one with string => value pairs and another with int => value pairs) while keeping the string/int keys? None of them will ever overlap (because one has only strings and the other has only integers).

Here is my current code (which doesn’t work, because array_merge is re-indexing the array with integer keys):

php array merge с сохранением ключей. Смотреть фото php array merge с сохранением ключей. Смотреть картинку php array merge с сохранением ключей. Картинка про php array merge с сохранением ключей. Фото php array merge с сохранением ключей

php array merge с сохранением ключей. Смотреть фото php array merge с сохранением ключей. Смотреть картинку php array merge с сохранением ключей. Картинка про php array merge с сохранением ключей. Фото php array merge с сохранением ключей

6 Answers 6

You can simply ‘add’ the arrays:

Considering that you have

The first array from sum will have values in the final output.

While this question is quite old I just want to add another possibility of doing a merge while keeping keys.

The result will be:

php array merge с сохранением ключей. Смотреть фото php array merge с сохранением ключей. Смотреть картинку php array merge с сохранением ключей. Картинка про php array merge с сохранением ключей. Фото php array merge с сохранением ключей

Two arrays can be easily added or union without chaning their original indexing by + operator. This will be very help full in laravel and codeigniter select dropdown.

php array merge с сохранением ключей. Смотреть фото php array merge с сохранением ключей. Смотреть картинку php array merge с сохранением ключей. Картинка про php array merge с сохранением ключей. Фото php array merge с сохранением ключей

Try array_replace_recursive or array_replace functions

Hello year 2010 question.

The OP.’s requirement is preserve keys (keep keys) and not overlap (I think overwrite). In some case such as numeric keys it is possible but if string keys it seems to be not possible.

Custom function.

Here is my function that I just wrote to work on the same requirements. You are free to use for any purpose.

Источник

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 с сохранением ключей 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 с сохранением ключей 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;
>

Источник

array_merge

array_merge — Сливает один или большее количество массивов

Описание

Сливает элементы одного или большего количества массивов таким образом, что значения одного массива присоединяются к концу предыдущего. Результатом работы функции является новый массив.

Если входные массивы имеют одинаковые строковые ключи, тогда каждое последующее значение будет заменять предыдущее. Однако, если массивы имеют одинаковые числовые ключи, значение, упомянутое последним, не заменит исходное значение, а будет добавлено в конец массива.

В результирующем массиве значения исходного массива с числовыми ключами будут перенумерованы в возрастающем порядке, начиная с нуля.

Список параметров

Возвращаемые значения

Возвращает результирующий массив. Если вызывается без аргументов, возвращает пустой массив ( array ).

Список изменений

ВерсияОписание
7.4.0Функция теперь может быть вызвана без каких-либо параметров. Ранее требовался хотя бы один параметр.

Примеры

Пример #1 Пример использования array_merge()

Результат выполнения данного примера:

Пример #2 Простой пример использования array_merge()

Помните, что числовые ключи будут перенумерованы!

Если вы хотите дополнить первый массив элементами второго без перезаписи элементов первого массива и без переиндексации, используйте оператор объединения массивов + :

Ключи из первого массива будут сохранены. Если ключ массива существует в обоих массивах, то будет использован элемент из первого массива, а соответствующий элемент из второго массива будет проигнорирован.

Пример #3 Пример использования array_merge() с не массивами

Результат выполнения данного примера:

Смотрите также

Источник

Памятка по сложению массивов

Одномерные массивы в PHP можно складывать с помощью функции array_merge() и оператора сложения «+». И каждый раз приходится вспоминать, в чем разница в использовании этих двух способов сложения массивов.

Сложение массивов с помощью array_merge(),
когда ключи не важны

Повторяющиеся значения нужно удалить с помощью функции array_unique(), и заготовка для слияния значений массивов будет выглядеть так:

Сложение массивов с сохранением ключей

Сложение массивов с помощью функции array_replace()

Если ключи важны (хоть ассоциативные, хоть числовые) и вам нужно обновить элементы одного массива элементами из других массивов, используйте функцию array_replace().

Сложение массивов с помощью оператора «+»

Этот способ сложения сложения массивов аналогичен предыдущему. Следует только обратить внимание на то, что здесь, чтобы обновить старый массив новым, нужно к новому массиву прибавить старый, что немного ломает мозг. Преимущества этого способа сложения — он производительнее и короче в записи.

Полезный пример использования такого сложения — массив в качестве параметра функции, когда значения по умолчанию обновляются входными данными:

Arr::mergeByKey() — сложение многомерных массивов

Внимание! Выше были приведены примеры только для одномерных массивов. Мы рекомендуем вам пользоваться универсальным методом Arr::mergeByKey(), который складывает по ключам массивы любого типа и любой размерности.

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *