Coder Social home page Coder Social logo

remapkeys's Introduction

Remap Keys

Tests

๐Ÿ’ฟ composer require dakujem/remapkeys

Functions for common array operations.

This package adds a pair of functions similar to array_map that are commonly used when working with arrays:

  • array_remap
    • like array_map, but allows to specify/map indexes of the result
  • array_map_keys
    • like array_map, but passes indexes to the iteratee function and preserves indexes in the result

array_remap

Allows re-mapping both indices and values of arrays using a mapper function.

$input = [
    'foo' => 'bar',
    'b' => 'Bran',
    'unknown' => 'Stark',
];
array_remap(function($val, $index){
    return [ strtolower($val) => strlen($index) ];
}, $input);

/* result:
[
    'bar' => 3,
    'bran' => 1,
    'stark' => 7,
]
*/
$input = [
    [
        'url' => 'https://www.google.com',
        'provider' => 'Google'
    ],
    [
        'url' => 'https://www.yahoo.com',
        'provider' => 'Yahoo!'
    ],
];
array_remap(function($val){
    return [ $val['url'] => $val['provider'] ];
}, $input);

/* result:
[
    'https://www.google.com' => 'Google',
    'https://www.yahoo.com' => 'Yahoo!',
]
*/

Internally, this is a map-reduce operation.

See the source for more details.

array_map_keys

Allows to work with both array values and their indexes. The indexes are preserved in the result.

$input = [
    'foo' => 'bar',
    'boring' => 'Bran',
    'strange' => 'Stark',
];
array_map_keys(function($val, $index){
    return ucfirst($index) . ' ' . ucfirst($val);
}, $input);

/* result:
[
    'foo' => 'Foo Bar',
    'boring' => 'Boring Bran',
    'strange' => 'Strange Stark',
]
*/

Note that one could natively call array_map($values, array_keys($values)), but that call does not preserve the original keys.

See the source for more details.

Why

These two fill the gap in PHP core for commonly occurring operations when the indexes are used during mapping.
A seemingly simple task, it has its caveats when implementing, though.

remapkeys's People

Contributors

dakujem avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.