Skip to content

danielsdeboer/array-map-keys

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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