PHP code example of phiil / xtraverse

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

    

phiil / xtraverse example snippets


$data = [
    'blocks' => [
        [
            'id' => 1,
            'title' => 'First block',
        ],
    ],
];

$path = 'blocks[1].title';

use Phiil\XTraverse\Traverser;

$traverser = new Traverser();
$title = $traverser->traverseData($path, $data, traverseArrayLimit: false); // we want a non-array value - pass "false" as the last argument or the service will throw an exception

echo $title;

use Phiil\XTraverse\Traverser;

// We want to update the title of the block we previously traversed to
$updatePath = 'blocks[1].title';

$traverser = new Traverser();
$data = $traverser->update($data, $path, 'New title')->data;

use Phiil\XTraverse\Traverser;

$object = [
    'id' => null,
    'title' => 'Second block',
];
$traverser = new Traverser();
$data = $traverser->update($object, 'blocks.$', $object)->data;
$data