PHP's array_map()
function doesn't allow associative array key mutation. This package provides a function, array_map_keys()
, which does.
The function iterates over an array and mutates each array item with the provided callback.
Via Composer:
composer require aviator/array-map-keys
Via Composer:
composer test
An array:
$input = [
[
'company' => 'Aviator Creative',
'owner' => 'Daniel Deboer',
'email' => 'daniel.s.deboer@gmail.com',
],
[
'company' => 'Widget Makers',
'owner' => 'Jane Doe',
'email' => 'jane@widgets.com',
],
];
A callback:
$callback = function ($key, $value) {
return [
$value['owner'] => $value['email'];
];
};
The array_map_keys
function:
$results = array_map_keys($input, $callback);
The output:
echo $results;
/*
[
'Daniel Deboer' => 'daniel.s.deboer@gmail.com',
'Jane Doe' => 'jane@widgets.com',
]
*/
This package is licensed with the MIT License (MIT).