PHP code example of rikta / value-path

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

    

rikta / value-path example snippets


$data = [
    [
        'name' => 'John Doe',
        'contact' => [
            'phone' => [
                '+49301234567'
            ]
        ]
    ]
];

$path = new Rikta\ValuePath\ValuePath('.contact.phone.0');
$phone = $path($data);

\PHPUnit\Framework\assertEquals('+49301234567', $phone);

new Rikta\ValuePath\ValuePath('.something'); // = $value['something']
new Rikta\ValuePath\ValuePath('.0'); // = $value[0]
new Rikta\ValuePath\ValuePath('["something"]'); // = $value['something']
new Rikta\ValuePath\ValuePath('->something'); // = $value->something
new Rikta\ValuePath\ValuePath('->something("a", "b", 1)]'); // = $value->something("a", "b", 1)

// it's also possible to chain the notations to get some deeply nested data
new Rikta\ValuePath\ValuePath('.something["somethingElse"]->someOtherThing->sth("a", "b")]');

// ' and " are interchangeable
new Rikta\ValuePath\ValuePath(".something['somethingElse']->someOtherThing->sth('a', 'b', 1)]");