PHP code example of adbario / php-dot-notation
1. Go to this page and download the library: Download adbario/php-dot-notation 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/ */
adbario / php-dot-notation example snippets
$array['info']['home']['address'] = 'Kings Square';
echo $array['info']['home']['address'];
// Kings Square
$dot->set('info.home.address', 'Kings Square');
echo $dot->get('info.home.address');
$dot['info.home.address'] = 'Kings Square';
echo $dot['info.home.address'];
$dot = new \Adbar\Dot;
// With existing array
$dot = new \Adbar\Dot($array);
// Or with auto parsing dot notation keys in existing array
$dot = new \Adbar\Dot($array, true);
// You can also set a custom delimiter instead of the default dot (.)
$dot = new \Adbar\Dot($array, false, "_");
$dot = dot();
// With existing array
$dot = dot($array);
// Or with auto parsing dot notation keys in existing array
$dot = dot($array, true);
// You can also set a custom delimiter instead of the default dot (.)
$dot = dot($array, true, "_");
$dot = dot();
$dot->add('user.name', 'John')
->set('user.email', '[email protected] ')
->clear(); // returns empty Dot
$dot->add('user.name', 'John');
// Equivalent vanilla PHP
if (!isset($array['user']['name'])) {
$array['user']['name'] = 'John';
}
$dot->add([
'user.name' => 'John',
'page.title' => 'Home'
]);
$values = $dot->all();
$dot->clear('user.settings');
// Equivalent vanilla PHP
$array['user']['settings'] = [];
$dot->clear(['user.settings', 'app.config']);
$dot->clear();
// Equivalent vanilla PHP
$array = [];
$dot->count('user.siblings');
$dot->count();
// Or use count() function as Dot implements Countable
count($dot);
$dot->delete('user.name');
// ArrayAccess
unset($dot['user.name']);
// Equivalent vanilla PHP
unset($array['user']['name']);
$dot->delete([
'user.name',
'page.title'
]);
$flatten = $dot->flatten();
echo $dot->get('user.name');
// ArrayAccess
echo $dot['user.name'];
// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;
// Equivalent vanilla PHP >= 7.0
echo $array['user']['name'] ?? null;
echo $dot->get('user.name', 'some default value');
$dot->has('user.name');
// ArrayAccess
isset($dot['user.name']);
$dot->has([
'user.name',
'page.title'
]);
$dot->isEmpty('user.name');
// ArrayAccess
empty($dot['user.name']);
// Equivalent vanilla PHP
empty($array['user']['name']);
$dot->isEmpty([
'user.name',
'page.title'
]);
$dot->isEmpty();
$dot->merge($array);
// Equivalent vanilla PHP
array_merge($originalArray, $array);
$dot->merge('user', $array);
// Equivalent vanilla PHP
array_merge($originalArray['user'], $array);
$dot->mergeRecursive($array);
// Equivalent vanilla PHP
array_merge_recursive($originalArray, $array);
$dot->mergeRecursive('user', $array);
// Equivalent vanilla PHP
array_merge_recursive($originalArray['user'], $array);
$dot->mergeRecursiveDistinct($array);
$dot->mergeRecursiveDistinct('user', $array);
echo $dot->pull('user.name');
// Equivalent vanilla PHP < 7.0
echo isset($array['user']['name']) ? $array['user']['name'] : null;
unset($array['user']['name']);
// Equivalent vanilla PHP >= 7.0
echo $array['user']['name'] ?? null;
unset($array['user']['name']);
echo $dot->pull('user.name', 'some default value');
$items = $dot->pull();
$dot->push('users', 'John');
// Equivalent vanilla PHP
$array['users'][] = 'John';
$dot->push('John');
// Equivalent vanilla PHP
$array[] = 'John';
$dot->replace($array);
// Equivalent vanilla PHP
array_replace($originalArray, $array);
$dot->merge('user', $array);
// Equivalent vanilla PHP
array_replace($originalArray['user'], $array);
$dot->set('user.name', 'John');
// ArrayAccess
$dot['user.name'] = 'John';
// Equivalent vanilla PHP
$array['user']['name'] = 'John';
$dot->set([
'user.name' => 'John',
'page.title' => 'Home'
]);
$dot->setArray($array);
$dot->setReference($array);
echo $dot->toJson('user');
echo $dot->toJson();
composer