How to Merge Array and Preserve Keys

How to merge array and preserve keys?

You're looking for array_replace():

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'e' => 5, 'f' => 6, 'a' => 'new value', '123' => 456);
print_r(array_replace($array1, $array2));

Available since PHP 5.3.

Update

You can also use the union array operator; it works for older versions and might actually be faster too:

print_r($array2 + $array1);

PHP Merge Array and keep key

use array_replace_recursive

$array = array_replace_recursive($a1, $a2);

How can I merge two associative arrays and preserve the global order of the entries?

Note that this solution will only work if the two arrays have the same length:

$arr1 = [ 'a' => '1', 'b' => 2 ];
$arr2 = [ 'h' => 'c', 'j' => '3' ];

$count = count($arr1);
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);

$result = [];
for ($i = 0; $i < $count; $i++) {
$key1 = $keys1[$i];
$result[$key1] = $arr1[$key1];
$key2 = $keys2[$i];
$result[$key2] = $arr2[$key2];
}

print_r($result);

Output:

Array
(
[a] => 1
[h] => c
[b] => 2
[j] => 3
)

Edited based on mickmackusa's comment below.

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

You can simply 'add' the arrays:

>> $a = array(1, 2, 3);
array (
0 => 1,
1 => 2,
2 => 3,
)
>> $b = array("a" => 1, "b" => 2, "c" => 3)
array (
'a' => 1,
'b' => 2,
'c' => 3,
)
>> $a + $b
array (
0 => 1,
1 => 2,
2 => 3,
'a' => 1,
'b' => 2,
'c' => 3,
)

merge two arrays, while maintaining the numeric keys

Try using the + operator.

$one = array(2 => 'abc', 5 => 'cde');
$two = array(32 => 'fge', 13 => 'def');
$three = $one + $two;

$three should now look like this:

[2] = abc
[5] = cde
[32] = fge
[13] = def

How to merge two arrays and sum the values of duplicate keys?

I think the problem is simpler than it looks. You really only need a conditional to preclude undefined offset notices. Just iterate all keys and values in both arrays and add the values to the corresponding key in the merged array.

foreach ([$a1, $a2] as $a) {                             // iterate both arrays
foreach ($a as $key => $value) { // iterate all keys+values
$merged[$key] = $value + ($merged[$key] ?? 0); // merge and add
}
}

Really, the line that actually does the addition ($merged[$key] = $value + ($merged[$key] ?? 0);) could be reduced to $merged[$key] += $value;. That would still work, but it would produce a bunch of undefined offset notices. So instead we can set the key equal to the value plus either the previous value (if it exists) or zero.

If you're still using PHP 5, you can use a ternary instead of the null coalescing operator (??), like this:

$merged[$key] = $value + (isset($merged[$key]) ? $merged[$key] : 0);

The output won't be in the same order shown in your desired result, but you can use ksort($merged); to accomplish that

PHP : merge two arrays, but keep only keys from the first one

$array3 = array_merge(
$array1,
array_intersect_key($array2, $array1)
);

How to merge multidimensional arrays while preserving keys?

Have you considered array_replace_recursive()?

print_r(array_replace_recursive($array1, $array2, $array3, $array4));

Combine (merge) 2 arrays by keys and change keys name in the result array

You can use array_map (doc) and array_combine (doc) as:

$res = array_map(null, $valuesArray, $numbersArray);
$keys = array("Value", "Number");
$res = array_map(function ($e) use ($keys) {return array_combine($keys, $e);}, $res);

Notice the use of null in array_map. From documentation:

An interesting use of this function is to construct an array of arrays, which can be easily performed by using NULL as the name of the callback function

This way you can merge more arrays - just remember to add the correct key to $keys

Live example: 3v4l



Related Topics



Leave a reply



Submit