PHP code example of laurynasgadl / travers

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

    

laurynasgadl / travers example snippets


$params = [
    'fruits' => [
        'apple' => 1,
        'avocado' => 1.1,
        'banana' => [
            'baby' => 'baby',
            'ice_cream' => true,
            'casual' => false,
        ],
    ],
];

Travers::get('fruits.apple', $params); // 1
Travers::get('fruits.banana.ice_cream', $params); // true
Travers::get('fruits.banana.casual', $params); // false
Travers::get('fruits.apple.red', $params); // null

use Luur\Travers;

$data = [
    'path' => 1,
];

$travers = new Travers($data);

$val = $travers->find('path'); // 1
$val = Travers::get('path', $data); // 1

$shouldThrowException = true;
$customDelimiter = '=';

$travers = new Travers($data, $shouldThrowException); // If a path is not found, will throw exception instead of returning null
$travers = new Travers($data, false, $customDelimiter); // Will separate path branches by the provided delimiter

$data = [
    'path' => 1,
];

$travers = new Travers($data);

$data = $travers->change('path', 2); // ['path' => 2]
$data = Travers::set('path', 3, $data); // ['path' => 3]

$data = Travers::set('path.new.trail', 4, $data);

/**
 * $data = [
 *      'path' => [
 *          'new' => [
 *              'trail' => 4,
 *          ],
 *      ],
 * ];
 */