2017 © Pedro Peláez
 

library arraypath

php array utility to manipulate array in a xpath way

image

mathiasgrimm/arraypath

php array utility to manipulate array in a xpath way

  • Tuesday, April 26, 2016
  • by mathiasgrimm
  • Repository
  • 2 Watchers
  • 25 Stars
  • 10,399 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 13 Versions
  • 7 % Grown

The README.md

ArrayPath

Author Latest Version Total Downloads, (*1)

ArrayPath is an easy and very convenient way for manipulating arrays, especially multidimensional.
Forget about checking for existing indexes and/or getting an E_NOTICE.
, (*2)

With ArrayPath you can easily Check, Add, Remove and Retrieve elements from any array, (*3)

Our examples will be using a class alias which will be explained next, (*4)

Using Class Alias

The default class alias is A but you can also define your custom alias., (*5)

By using the default alias you get the benefits of the ide auto-completion, (*6)

<?php
// recommended
ArrayPath::registerClassAlias();

A::get($aData, 'a/b/c');

// or

ArrayPath::registerClassAlias('MyAlias');
MyAlias::get($aData, 'a/b/c');

A good place to register the class alias is in any bootstrap file like a Service Provider or initialisation script, (*7)

Example 1 (Get)

<?php
$post = array(
    'user' => array(
        'basicInformation' => array(
            'name'    => 'Mathias',
            'surname' => 'Grimm'
        ),
    )
);

// normal php way
$sName    = isset($post['user']['basicInformation']['name'   ]) ? $post['user']['basicInformation']['name'   ] : null;
$sSurname = isset($post['user']['basicInformation']['surname']) ? $post['user']['basicInformation']['surname'] : null;

// default value
$sLocale = isset($post['user']['locale']) ? $post['user']['locale'] : 'Europe/Dublin';

// ===================================================================

// ArrayPath
$sName    = A::get($post, 'user/basicInformation/name');
$sSurname = A::get($post, 'user/basicInformation/surname');

// with default value
$sLocale  = A::get($post, 'user/locale', 'Europe/Dublin');

Example 2 (Set)

<?php
// normal php way
$aUser = array();
$sName = $aUser['user']['basicInformation']['name'] = 'Mathias Grimm';
// ===================================================================

// ArrayPath
$aUser = array();
$sName = A::set($aUser, 'user/basicInformation/name', 'Mathias');

Example 3 (Exists)

The exists checks for the existence of the index as in array_key_exists
and returns true if the key exists regardless the value.
An isset will return false in case of a null value., (*8)

<?php
// normal php way
$bExists = false;
if (array_key_exists('user', (array) $aUser)) {
    if (array_key_exists('basicInformation', (array) $aUser['user'])) {
        if (array_key_exists('name', (array) $aUser['user']['basicInformation'])) {
            $bExists = true;
        }
    }
}

// ===================================================================

// ArrayPath
$bExists = A::exists($aUser, 'user/basicInformation/name');

Example 4 (Get and Remove)

<?php
// normal php way
if (isset($aUser['user']['basicInformation']['name'])) {
    $sName = $aUser['user']['basicInformation']['name'];
    unset($aUser['user']['basicInformation']['name']);
}


// ArrayPath
$sName = A::remove($aUser, 'user/basicInformation/name');

Example 5 (Using a custom separator)

<?php
ArrayPath::setSeparator('.');
$sName = A::get($aUser, 'user.basicInformation.name');

ArrayPath::setSeparator('-');
$sName = A::get($aUser, 'user-basicInformation-name');

ArrayPath::setSeparator('->');
$sName = A::get($aUser, 'user->basicInformation->name');

ArrayPath::setSeparator('|');
$sName = A::get($aUser, 'user|basicInformation|name');

Parameters Consistency

The parameters will always be in this sequence, when available:, (*9)

$arrayData, $index, $value, $default, (*10)

Composer/Packagist

https://packagist.org/packages/mathiasgrimm/arraypath, (*11)

"require": {
    "mathiasgrimm/arraypath": "2.*"
}

The Versions

26/04 2016

dev-master

9999999-dev https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

26/04 2016

v2.0.7

2.0.7.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

22/03 2016

v2.0.6

2.0.6.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

21/03 2016

v2.0.5

2.0.5.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

15/03 2016

v2.0.4

2.0.4.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

15/03 2016

v2.0.3

2.0.3.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

15/03 2016

v2.0.2

2.0.2.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

15/03 2016

v2.0.1

2.0.1.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

15/03 2016

v2.0.0

2.0.0.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

22/03 2015

v1.3

1.3.0.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

  • php >=5.3.0

 

The Development Requires

php array xpath util array_key_exists_recursive

20/03 2015

v1.2

1.2.0.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

 

The Development Requires

php array xpath util array_key_exists_recursive

20/03 2015

v1.1

1.1.0.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

 

The Development Requires

php array xpath util array_key_exists_recursive

13/10 2014

1.0

1.0.0.0 https://github.com/mathiasgrimm/arraypath/

php array utility to manipulate array in a xpath way

  Sources   Download

The Requires

 

The Development Requires

php array xpath util array_key_exists_recursive