https://github.com/danielsdeboer/array-map-keys
Map over an array with access to keys
https://github.com/danielsdeboer/array-map-keys
arrays keys map php php7
Last synced: 6 months ago
JSON representation
Map over an array with access to keys
- Host: GitHub
- URL: https://github.com/danielsdeboer/array-map-keys
- Owner: danielsdeboer
- License: mit
- Created: 2017-11-25T15:30:15.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-25T16:18:38.000Z (almost 8 years ago)
- Last Synced: 2024-09-14T12:52:48.153Z (about 1 year ago)
- Topics: arrays, keys, map, php, php7
- Language: PHP
- Size: 3.91 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://packagist.org/packages/aviator/array-map-keys)
[](https://packagist.org/packages/aviator/array-map-keys)
[](https://travis-ci.org/danielsdeboer/array-map-keys)## 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:
```php
$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:
```php
$callback = function ($key, $value) {
return [
$value['owner'] => $value['email'];
];
};
```The `array_map_keys` function:
```php
$results = array_map_keys($input, $callback);
```The output:
```php
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)](LICENSE.md).