I have two arrays in PHP:
Array1
(
[0] => 1
[1] => 2
[2] => 2
)
Array2
(
[0] => 18
[1] => 19
[2] => 20
)
Array1
contains the Ids of Delivery Addresses.
Array2
contains the Ids of Contacts.
Array1
and Array2
are 'aligned' so that Contact 18 (Array2[0]
) resides at Delivery Address Id #1 (Array1[0]
) (and so on).
What I would like is use the unique values of Array1
as array keys for Array3
, and the values of Array2
used as the array values Array3
.
The end result being that Contacts are 'grouped' by their Delivery Address.
Like so:
Array 3
(
[1] = array (
[0] => 18
)
[2] = array (
[0] => 19
[1] => 20
)
)
$array3 = array();
foreach ( $array1 as $k => $v ) {
if ( !isset($array3[$v]) )
$array3[$v] = array();
$array3[$v][] = $array2[$k];
}
var_dump($array3);