PHP code example of mathiasgrimm / arraypath

1. Go to this page and download the library: Download mathiasgrimm/arraypath library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mathiasgrimm / arraypath example snippets



// recommended
ArrayPath::registerClassAlias();

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

// or

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



$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');



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

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


// 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');


// 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');


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');