PHP code example of gnugat / traversal

1. Go to this page and download the library: Download gnugat/traversal 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/ */

    

gnugat / traversal example snippets


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

$baz = Gnugat\Traversal\get_in($data, array('foo', 'bar', 'baz'));




$users = array(
    array('name' => 'Igor Wiedler'),
    array('name' => 'Jane Doe'),
    array('name' => 'Acme Inc'),
);

$name = Gnugat\Traversal\get_in($users, array(1, 'name'));
//= 'Jane Doe'

$data = array('foo' => 'bar'];

$baz = Gnugat\Traversal\get_in($data, array('baz'));
//= null

$data = array('foo' => 'bar');

$baz = Gnugat\Traversal\get_in($data, array('baz'), 'qux');
//= 'qux'

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

$new = Gnugat\Traversal\update_in($data, array('foo', 'answer'), $inc);
//= array('foo' => array('answer' => 43))

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

$new = Gnugat\Traversal\update_in($data, array('foo'), $concat, ' is the ', 'best');
//= array('foo' => 'bar is the best')

$data = array('foo' => 'bar');

$new = Gnugat\Traversal\assoc_in($data, array('foo'), 'baz');
//= array('foo' => 'baz')

$data = [];

$new = Gnugat\Traversal\assoc_in($data, array('foo', 'bar'), 'baz');
//= array('foo' => array('bar' => 'baz'))