2017 © Pedro Peláez
 

library get-in

Functions for hash map (assoc array) traversal.

image

igorw/get-in

Functions for hash map (assoc array) traversal.

  • Wednesday, January 7, 2015
  • by igorw
  • Repository
  • 10 Watchers
  • 282 Stars
  • 2,365,084 Installations
  • PHP
  • 28 Dependents
  • 0 Suggesters
  • 20 Forks
  • 2 Open issues
  • 5 Versions
  • 8 % Grown

The README.md

get-in

Build Status, (*1)

Functions for hash map (assoc array) traversal., (*2)

When dealing with nested associative structures, traversing them can become quite a pain. Mostly because of the amount of isset checking that is necessary., (*3)

For example, to access a nested key ['foo']['bar']['baz'], you must do something like this:, (*4)

$baz = (isset($data['foo']['bar']['baz'])) ? $data['foo']['bar']['baz'] : null;

Enough already! get-in provides a better way:, (*5)

$baz = igorw\get_in($data, ['foo', 'bar', 'baz']);

Installation

Through composer:, (*6)

$ composer require igorw/get-in:~1.0

Usage

get_in

Retrieve value from a nested structure using a list of keys:, (*7)

$users = [
    ['name' => 'Igor Wiedler'],
    ['name' => 'Jane Doe'],
    ['name' => 'Acme Inc'],
];

$name = igorw\get_in($users, [1, 'name']);
//= 'Jane Doe'

Non existent keys return null:, (*8)

$data = ['foo' => 'bar'];

$baz = igorw\get_in($data, ['baz']);
//= null

You can provide a default value that will be used instead of null:, (*9)

$data = ['foo' => 'bar'];

$baz = igorw\get_in($data, ['baz'], 'qux');
//= 'qux'

update_in

Apply a function to the value at a particular location in a nested structure:, (*10)

$data = ['foo' => ['answer' => 42]];
$inc = function ($x) {
    return $x + 1;
};

$new = igorw\update_in($data, ['foo', 'answer'], $inc);
//= ['foo' => ['answer' => 43]]

You can variadically provide additional arguments for the function:, (*11)

$data = ['foo' => 'bar'];
$concat = function (/* $args... */) {
    return implode('', func_get_args());
};

$new = igorw\update_in($data, ['foo'], $concat, ' is the ', 'best');
//= ['foo' => 'bar is the best']

assoc_in

Set a value at a particular location:, (*12)

$data = ['foo' => 'bar'];

$new = igorw\assoc_in($data, ['foo'], 'baz');
//= ['foo' => 'baz']

It will also set the value if it does not exist yet:, (*13)

$data = [];

$new = igorw\assoc_in($data, ['foo', 'bar'], 'baz');
//= ['foo' => ['bar' => 'baz']]

Inspiration

The naming and implementation is inspired by the get-in, update-in and assoc-in functions from clojure., (*14)

The Versions

07/01 2015

dev-master

9999999-dev

Functions for hash map (assoc array) traversal.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

hash-map assoc-array

16/12 2014

v1.0.3

1.0.3.0

Functions for for hash map (assoc array) traversal.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

hash-map assoc-array

03/09 2014

v1.0.2

1.0.2.0

Functions for for hash map (assoc array) traversal.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

hash-map assoc-array

06/07 2014

v1.0.1

1.0.1.0

Functions for for hash map (assoc array) traversal.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

hash-map assoc-array

08/01 2014

v1.0.0

1.0.0.0

Functions for for hash map (assoc array) traversal.

  Sources   Download

MIT

The Requires

  • php >=5.4

 

hash-map assoc-array