PHP: Merge Two Arrays While Keeping Keys Instead of Reindexing

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,
)

PHP Merge Array and keep key

use array_replace_recursive

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

Combine two arrays

Just use:

$output = array_merge($array1, $array2);

That should solve it. Because you use string keys if one key occurs more than one time (like '44' in your example) one key will overwrite preceding ones with the same name. Because in your case they both have the same value anyway it doesn't matter and it will also remove duplicates.

Update: I just realised, that PHP treats the numeric string-keys as numbers (integers) and so will behave like this, what means, that it renumbers the keys too...

A workaround is to recreate the keys.

$output = array_combine($output, $output);

Update 2: I always forget, that there is also an operator (in bold, because this is really what you are looking for! :D)

$output = $array1 + $array2;

All of this can be seen in:
http://php.net/manual/en/function.array-merge.php

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

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

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

Merging two arrays without changing key values php

From the documentation, Example #3:

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator:

<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);
?>

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key's element from the second array will be ignored.

array(5) {
[0]=>
string(6) "zero_a"
[2]=>
string(5) "two_a"
[3]=>
string(7) "three_a"
[1]=>
string(5) "one_b"
[4]=>
string(6) "four_b"
}

Therefore, try: $a[0] + $b[0]

Merge two associative arrays without reindexing - PHP

use array_replace_recursive to add invite items to corresponding source array elements

array_replace_recursive($a1, $a2);

demo

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.



Related Topics



Leave a reply



Submit