PHP code example of smoren / nested-accessor
1. Go to this page and download the library: Download smoren/nested-accessor 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/ */
smoren / nested-accessor example snippets
use Smoren\NestedAccessor\Components\NestedAccessor;
$source = [
'data' => [
'id' => 1,
'name' => 'Countries classifier',
'extra' => null,
'country_names' => ['Russia', 'Belarus'],
],
'countries' => [
[
'name' => 'Russia',
'cities' => [
[
'name' => 'Moscow',
'extra' => [
'codes' => [
['value' => 7495],
['value' => 7499],
],
],
],
[
'name' => 'Petersburg',
'extra' => [
'codes' => [
['value' => 7812],
],
],
],
],
],
[
'name' => 'Belarus',
'cities' => [
[
'name' => 'Minsk',
'extra' => [
'codes' => [
['value' => 375017],
],
],
],
],
],
]
];
$accessor = new NestedAccessor($input);
echo $accessor->get('data.name'); // 'Countries classifier'
print_r($accessor->get('countries.name')); // ['Russia', 'Belarus']
print_r($accessor->get('countries.cities.name')); // ['Moscow', 'Petersburg', 'Minsk']
print_r($accessor->get('countries.cities.extra.codes.value')); // [7495, 7499, 7812, 375017]
var_dump($accessor->isset('data.name')); // true
var_dump($accessor->isset('data.extra')); // false
var_dump($accessor->isset('this.path.not.exist')); // false
var_dump($accessor->exist('data.name')); // true
var_dump($accessor->exist('data.extra')); // true
var_dump($accessor->exist('this.path.not.exist')); // false
$accessor->set('data.name', 'New name');
echo $accessor->get('data.name'); // 'New name'
$accessor->append('data.country_names', 'Mexico');
echo $accessor->get('data.country_names'); // ['Russia', 'Belarus', 'Mexico']
$accessor->delete('data.name');
var_dump($accessor->exist('data.name')); // false