'Zip multiple arrays in PHP based on their identical keys and not their indices or their positions in array
I have two arrays:
$a = ['one'=>1,'two'=> 2,'three'=> 3,'four'=> 4,'five'=> 5, 'six' => 6];
$b = ['one'=>'uno', 'three'=>'tres','two'=> 'dos', 'four'=>'cuatro', 'five'=>'cinco'];
note that in $b Array 'three' and 'two' keys are not in same order as $a. also 'six' key is not available in $b Array.
I want this result as output:
Array
(
[0] => Array
(
[0] => 1
[1] => uno
)
[1] => Array
(
[0] => 2
[1] => dos
)
[2] => Array
(
[0] => 3
[1] => tres
)
[3] => Array
(
[0] => 4
[1] => cuatro
)
[4] => Array
(
[0] => 5
[1] => cinco
)
)
I tried to use array_map but it don't detect keys and just zip them based of their indices:
$d = array_map(null, $a, $b);
Solution 1:[1]
Can't you just use ksort(). Then, create a new array using `foreach'?
Solution 2:[2]
An example using foreach
<?php
$a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5];
$b = ['one' => 'uno', 'three' => 'tres', 'two' => 'dos', 'four' => 'cuatro', 'five' => 'cinco'];
$output = [];
foreach ($a as $key => $value) {
$output[] = [$value, $b[$key]];
}
print_r($output);
Solution 3:[3]
modified ikhvjs answer (when keys in both arrays are not same as each others):
$a = ['one' => 1, 'two' => 2, 'four' => 4, 'five' => 5, 'six' => 6];
$b = ['one' => 'uno', 'three' => 'tres', 'two' => 'dos', 'four' => 'cuatro', 'five' => 'cinco'];
$r=array_intersect_key($a,$b);
$output = [];
foreach ($r as $key => $value) {
$output[] = [$value, $b[$key]];
}
echo "<pre>";
print_r($output);
echo "</pre>";
Solution 4:[4]
I must say @ikhvjs's answer is the closest to perfect, but the $b array doesn't have a value with the key six. To accommodate the possibility of elements in $b not relating to elements in $a, use the null coalescing operator to fallback to a null value.
As for the mentioning of ksort(), this will do nothing to accommodate the fact that the input arrays have different lengths / keys.
The use of array_intersect_key() will result in $a values being omitted where not related to an element in $b.
If $a is not a reliable "master" array (with all possible keys), then you will need to loop a temporary array formed by merging the two arrays and implement the null coalescing operator on both values to be pushed into the result array.
Code: (Demo) (Demo of null coalescing on both values)
$a = [
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6
];
$b = [
'one' => 'uno',
'three' => 'tres',
'two' => 'dos',
'four' => 'cuatro',
'five' => 'cinco'
];
$result = [];
foreach ($a as $k => $v) {
$result[] = [$v, $b[$k] ?? null];
}
var_export($result);
Output:
array (
0 =>
array (
0 => 1,
1 => 'uno',
),
1 =>
array (
0 => 2,
1 => 'dos',
),
2 =>
array (
0 => 3,
1 => 'tres',
),
3 =>
array (
0 => 4,
1 => 'cuatro',
),
4 =>
array (
0 => 5,
1 => 'cinco',
),
5 =>
array (
0 => 6,
1 => NULL,
),
)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ?ukasz Socha |
| Solution 2 | ikhvjs |
| Solution 3 | |
| Solution 4 | mickmackusa |
