Giter Site home page Giter Site logo

arraypath's Introduction

ArrayPath

Author Latest Version Total Downloads

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.

With ArrayPath you can easily Check, Add, Remove and Retrieve elements from any array

Our examples will be using a class alias which will be explained next

Using Class Alias

The default class alias is A but you can also define your custom alias.

By using the default alias you get the benefits of the ide auto-completion

<?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

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.

<?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:

$arrayData, $index, $value, $default

Composer/Packagist

https://packagist.org/packages/mathiasgrimm/arraypath

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

arraypath's People

Contributors

mathiasgrimm avatar rdohms avatar darkspider avatar thotty avatar

Watchers

Lenon Leite avatar James Cloos 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.