PHP code example of peridot-php / object-path

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

    

peridot-php / object-path example snippets


$data = [
  'name' => 'Brian',
  'hobbies' => [
    'reading',
    'programming',
    'lion taming'  
  ],
  'address' => [
    'street' => '1234 Lane',
    'zip' => '12345'  
  ]
];

use Peridot\ObjectPath\ObjectPath;

$path = new ObjectPath($data);
$reading = $path->get('hobbies[0]');
$zip = $path->get('address[zip]');

// the result of get() is an ObjectPathValue instance
$value = $reading->getPropertyValue();

// The syntax also works for objects and nested structures

$data = new stdClass();

$data->name = 'Brian';
$data->address = new stdClass();
$data->address->zip = '12345';

$hobby = new stdClass();
$hobby->name = 'reading';
$hobby->style = 'relaxing';
$data->hobbies = [$hobby];

$path = new ObjectPath($data);
$name = $path->get('name');
$zip = $path->get('address->zip');
$reading = $path->get('hobbies[0]->name');