PHP code example of vicary / object-path

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

    

vicary / object-path example snippets


use Peridot\ObjectPath\ObjectPath;

// Works with nested arrays, objects, or a mixture of both.
$data = [
  'name' => 'Brian',
  'hobbies' => [
    'reading',
    'programming',
    'lion taming'
  ],
  'address' => new stdClass()
  [
    'street' => '1234 Lane',
    'zip' => '12345'
  ]
];

$data['address']->street = '1234 Lane';
$data['address']->zip = 12345;

// Wraps the value with ObjectPath
$path = new ObjectPath($data);

// Get the value directly
$reading = $path->{'hobbies[0]'};
$zip = $path->{'address[zip]'};

// Sets the value
$path->{'address->street'} = '12345 Street';

// Removes the property
unset($path->{'hobbies[2]'});



// backward compatible with peridot-php/object-path
$reading = $path->get('hobbies[0]');
$zip = $path->get('address[zip]');

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