PHP code example of jdz / data

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

    

jdz / data example snippets


use JDZ\Utils\Data;

$data = new Data();

$data->set('user.name.first', 'John');
$data->set('user.name.last', 'Doe');

$firstName = $data->get('user.name.first'); // "John"
$lastName = $data->get('user.name.last', 'Default Name'); // "Doe"
$nonExistent = $data->get('user.nonexistent', 'Default Value'); // "Default Value"

if ($data->has('user.name.first')) {
    echo "First name exists.";
}

class YourData extends \JDZ\Utils\Data
{
    public function toDot(array $arrayData): array
    {
        return $this->flatten($arrayData);
    }
}

$yourData = new YourData();
$flattened = $yourData->toDot([ 'user' => [ 'name' => [ 'first' => 'John', 'last' => 'Doe' ] ] ]);
// [ 'user.name.first' => 'John', 'user.name.last' => 'Doe' ]

class YourData extends \JDZ\Utils\Data
{
    public function toArray(array $dotData): array
    {
        return $this->unflatten($dotData);
    }
}

$yourData = new YourData();
$array = $yourData->toArray([ 'user.name.first' => 'John', 'user.name.last' => 'Doe' ]);
// [ 'user' => [ 'name' => [ 'first' => 'John', 'last' => 'Doe' ] ] ]

$data->sets([
    'user.name.middle' => 'Edward',
    'user.age' => 30
], true);

$data->erase('user.name.middle');