so I have an array like this :-
$headers = array();
$headers[] = array("country" => "Netherlands", "city" => "Amsterdam");
$headers[] = array("country" => "Netherlands", "city" => "Tillsburg");
$headers[] = array("country" => "Sweden", "city" => "Stockholm");
I need it like :-
array("country" => "Netherlands", "city" => "Amsterdam,Tillsburg");
array("country" => "Sweden", "city" => "Stockholm");
How do I merge the values?
Here's what I have tried till now, but it doesn't seems to be working.
function merge($headers) {
$final = [];
foreach($headers as $current) {
if(!in_array($current['country'], $final)) {
$final[] = $current['country'];
}
}
foreach($headers as $current) {
$final[$current['country']]['city'] = $current['city'];
}
return $final;
}
Any help would be appreciated.