Skip to content

Latest commit

 

History

History
67 lines (57 loc) · 1.22 KB

reindex.md

File metadata and controls

67 lines (57 loc) · 1.22 KB

reindex

Description

Returns a new collection with the keys reindexed by $fn. Optionally $fn receive the key as the second argument.

Parameters

fn
Function to generate the key.
coll
Collection to be reindexed.

Examples

Extract the id of the collection and add this as the key:

<?php

use function Lambdish\Phunctional\reindex;

$users = [
    [
        'id'      => 100,
        'name'    => 'Rich',
        'surname' => 'Hickey'
    ],
    [
        'id'      => 22,
        'name'    => 'Kurt',
        'surname' => 'Cobain'
    ],
    [
        'id'      => 12,
        'name'    => 'Gru',
        'surname' => 'Minion'
    ],
];

return reindex(
    function (array $user) {
        return $user['id'];
    }, 
    $users
);
            
// => [
//        100 => [
//            'id'      => 100,
//            'name'    => 'Rich',
//            'surname' => 'Hickey'
//        ],
//        22 => [
//            'id'      => 22,
//            'name'    => 'Kurt',
//            'surname' => 'Cobain'
//        ],
//        12 => [
//            'id'      => 12,
//            'name'    => 'Gru',
//            'surname' => 'Minion'
//        ],
//    ];