PHP code example of prowebcraft / dot
1. Go to this page and download the library: Download prowebcraft/dot 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/ */
prowebcraft / dot example snippets
echo $data['info']['home']['address'];
echo $data->get('info.home.address');
echo $data['info.home.address'];
$array = [
'user' => [
'firstname' => 'John',
'lastname' => 'Smith'
],
'info' => [
'kids' => [
0 => 'Laura',
1 => 'Chris',
2 => 'Little Johnny'
],
'home' => [
'address' => 'Rocky Road 3'
]
]
];
$data = new \Prowebcraft\Dot;
$data = new \Prowebcraft\Dot($array);
$data->setArray($array);
$data->setReference($array);
$data->set('info.home.tel', '09-123-456-789');
// Array style
$data['info.home.tel'] = '09-123-456-789';
$data->set([
'user.haircolor' => 'blue',
'info.home.address' => 'Private Lane 1'
]);
echo $data->get('info.home.address');
// Default value if the path doesn't exist
echo $data->get('info.home.country', 'some default value');
// Array style
echo $data['info.home.address'];
$values = $data->all();
``
Get a value from a path and remove it:
$values = $data->pull();
$data->add('info.kids', 'Amy');
$data->add('info.kids', [
'Ben', 'Claire'
]);
if ($data->has('info.home.address')) {
// Do something...
}
// Array style
if (isset($data['info.home.address'])) {
// Do something...
}
$data->delete('info.home.address');
// Array style
unset($data['info.home.address']);
$data->delete([
'user.lastname', 'info.home.address'
]);
$data->clear('info.home');
$data->clear([
'user', 'info.home'
]);
$data->clear();
$kids = $data->sort('info.kids');
// Sort recursively
$info = $data->sort('info');
$sorted = $data->sort();
// Sort recursively
$sorted = $data->sort();
$data->name = 'John';
echo $data->name;
if (isset($data->name)) {
// Do something...
}
unset($data->name);