, (*1)
Overview
PHP's array_map()
function doesn't allow associative array key mutation. This package provides a function, array_map_keys()
, which does., (*2)
The function iterates over an array and mutates each array item with the provided callback., (*3)
Installation
Via Composer:, (*4)
composer require aviator/array-map-keys
Testing
Via Composer:, (*5)
composer test
Usage
An array:, (*6)
$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:, (*7)
$callback = function ($key, $value) {
return [
$value['owner'] => $value['email'];
];
};
The array_map_keys
function:, (*8)
$results = array_map_keys($input, $callback);
The output:, (*9)
echo $results;
/*
[
'Daniel Deboer' => 'daniel.s.deboer@gmail.com',
'Jane Doe' => 'jane@widgets.com',
]
*/
Other Stuff
License
This package is licensed with the MIT License (MIT)., (*10)