2017 © Pedro Peláez
 

library array-finder

ArrayFinder component

image

shudrum/array-finder

ArrayFinder component

  • Thursday, October 5, 2017
  • by shudrum
  • Repository
  • 1 Watchers
  • 13 Stars
  • 338,113 Installations
  • PHP
  • 10 Dependents
  • 0 Suggesters
  • 7 Forks
  • 1 Open issues
  • 5 Versions
  • 14 % Grown

The README.md

Build Status, (*1)

ArrayFinder Component

The ArrayFinder component allow you to manage large nested arrays with ease., (*2)

Here is a simple example that shows how to easily get a value from an array:, (*3)

use Shudrum\Component\ArrayFinder\ArrayFinder;

$arrayFinder = new ArrayFinder([
    'level_1' => [
        'level_2' => [
            'level_3' => 'value',
        ],
    ],
]);

$myValue = $arrayFinder->get('level_1.level_2.level_3');
// OR
$myValue = $arrayFinder['level_1.level_2.level_3'];

To install this package, you can simply use composer:, (*4)

composer require shudrum/array-finder

Documentation

Methods

get($path)

You can get a value following a path separated by a '.'., (*5)

use Shudrum\Component\ArrayFinder\ArrayFinder;

$arrayFinder = new ArrayFinder([
    'a' => [
        'b' => [
            'c' => 'value1',
        ],
        'value2',
    ],
    'value3',
]);

$myValue = $arrayFinder->get('a.b.c'); // value1
$myValue = $arrayFinder->get('a.0'); // value2
$myValue = $arrayFinder->get(0); // value3

If the path is null, all the content will be returned., (*6)

set($path, $value)

You can add a value to a specific path separated by a '.'. If the nested arrays does not exists, it will be created., (*7)

use Shudrum\Component\ArrayFinder\ArrayFinder;

$arrayFinder = new ArrayFinder();
$arrayFinder->set('a.b', 'value');

$arrayFinder->get(); // ['a' => ['b' => 'value]]

changeSeparator($separator)

If the default separator (.) does not fit to your needs, you can call this method to change it., (*8)

use Shudrum\Component\ArrayFinder\ArrayFinder;

$arrayFinder = new ArrayFinder([…]);

$myValue = $arrayFinder->changeSeparator('/');
$myValue = $arrayFinder->get('a/b/c');

Implementations

The ArrayFinder component implements some usefull interfaces:, (*9)

ArrayAccess

You can use this object like an array:, (*10)

use Shudrum\Component\ArrayFinder\ArrayFinder;

$arrayFinder = new ArrayFinder([…]);

$value = $arrayFinder['a.b'];
$arrayFinder['a.b.c'] = 'value';
unset($arrayFinder['a.b']);

Countable

You can use count on this object:, (*11)

use Shudrum\Component\ArrayFinder\ArrayFinder;

$arrayFinder = new ArrayFinder([…]);

count($arrayFinder);
count($arrayFinder['a.b']);

Iterator

You can iterate on this object:, (*12)

use Shudrum\Component\ArrayFinder\ArrayFinder;

$arrayFinder = new ArrayFinder([…]);

foreach ($arrayFinder as $key => $value) {
    // …
}

Serializable

You can easily serialize / unserialize this object., (*13)

Resources

You can run the unit tests with the following command:, (*14)

$ cd path/to/Shudrum/Component/ArrayFinder/
$ composer install
$ phpunit

The Versions

05/10 2017

dev-master

9999999-dev https://github.com/Shudrum/ArrayFinder

ArrayFinder component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Julien Martin

05/10 2017

dev-fix-ci

dev-fix-ci https://github.com/Shudrum/ArrayFinder

ArrayFinder component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Julien Martin

01/02 2016

v1.1.0

1.1.0.0 https://github.com/Shudrum/ArrayFinder

ArrayFinder component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Julien Martin

14/01 2016

v1.0.1

1.0.1.0 https://github.com/Shudrum/ArrayFinder

ArrayFinder component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Julien Martin

10/12 2015

v1.0.0

1.0.0.0 https://github.com/Shudrum/ArrayFinder

ArrayFinder component

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Julien Martin