aviator/array-map-keys

Array map with keys

0.1.0 2017-11-25 16:05 UTC

This package is auto-updated.

Last update: 2024-04-21 19:24:50 UTC


README

Latest Stable Version License Build Status

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).