aviator / array-map-keys
Array map with keys
Installs: 6 770
Dependents: 2
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=7.0.0
Requires (Dev)
- larapack/dd: ^1.1
- phpunit/phpunit: ~6.4.0
This package is auto-updated.
Last update: 2024-10-21 20:41:42 UTC
README
Overview
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.
Installation
Via Composer:
composer require aviator/array-map-keys
Testing
Via Composer:
composer test
Usage
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', ] */
Other Stuff
License
This package is licensed with the MIT License (MIT).