Let's say we have the following dataset:
$data = [
'Newcastle Upon Tyne, Tyne and Wear, England' => 18,
'Gateshead, Tyne and Wear, England' => 17,
'Sunderland, Tyne and Wear, England' => 3,
...
];
I want to map through the function and change the keys to look like this:
$data = [
'Newcastle Upon Tyne' => 18,
'Gateshead' => 17,
'Sunderland' => 3,
...
];
I thought I might be able to use array_walk like so...
return array_walk($this->_cities, function(&$key, $val) {
substr( $key, 0, strpos( $key, ',' ) ) = $b;
});
But that produces the error:
Can't use function return value in write context
So instead I'm doing it like this:
$cleaned_data = [];
foreach ( $this->_cities as $city => $count )
$cleaned_data[ substr( $city, 0, strpos( $city, ',' ) ) ] = $count;
return $cleaned_data;
Is it possible to do this using array_walk or a similar function?