I have:
['countryCode' => 11, 'postalCode' => 12345],
['countryCode' => 11, 'postalCode' => 12346],
['countryCode' => 11, 'postalCode' => 12347],
['countryCode' => 11, 'postalCode' => 12348],
['countryCode' => 11, 'postalCode' => 12349],
['countryCode' => 12, 'postalCode' => 12345],
['countryCode' => 12, 'postalCode' => 12346],
['countryCode' => 12, 'postalCode' => 12347],
['countryCode' => 12, 'postalCode' => 12348],
['countryCode' => 12, 'postalCode' => 12349],
];
But I want:
'11' => ['12345, 12346, 12347, 12348, 12349'], '12' => ['12345, 12346, 12347, 12348, 12349']
];
I've tried:
$countries = array_column($data, 'countryCode');
$countries = array_unique($countries);
$countries = array_fill_keys($countries, []);
function makeArrays(&$countries, $data){
foreach ($countries as $countryId => $postalCodes){
foreach ($data as $item) {
if ($item['countryCode'] == $countryId) {
array_push($postalCodes, $item['postalCode']);
}
}
}
return $countries;
}
makeArrays($countries, $data);
When I var_dump the makeArrays function though, the country ID keys are not populated with an array of postal codes like I would expect:
array(2) {
[11]=>
array(0) {
}
[12]=>
array(0) {
}
}
How do I get each key's array to populate with the expected values?