PHP code example of jbizzay / php-dot

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

    

jbizzay / php-dot example snippets



if (isset($data['level1']) && isset($data['level1']['level2'])) {
    $value = $data['level1']['level2']['key'];
} else {
    $value = null;
}



$dot = new Dot($data);
$value = $dot->get('level1.level2.key');



use Jbizzay\Dot;

// Create empty dot
$dot = new Dot;

// Or, initialize with an array of data
$data = [
  'stats' => [
    'web' => [
      'hits' => 99
    ],
    'mobile' => [

    ]
  ]
];

$dot = new Dot($data);



$dot->get(); // Returns full data array

$dot->get('stats.web.hits'); // Returns 99

$dot->get('stats.mobile.hits'); // Returns null

$dot->get('some.random.undefined.key'); // Returns null

 


$dot
  ->set('stats.web.last_updated', new DateTime)
  ->set('stats.web.allow_tracking', true)
  ->set('stats.web.hits', function ($hits) {
    $hits++;
    return $hits;
  });

$dot->get('stats.web');

/* Returns:
Array
(
    [hits] => 100
    [last_updated] => DateTime Object
        (
            [date] => 2017-07-21 14:50:34.000000
            [timezone_type] => 3
            [timezone] => America/Los_Angeles
        )

    [allow_tracking] => 1
)
*/




$dot
  ->unset('path.to.value')
  ->unset('some.other.value');



$dot->has('stats.web'); // true
$dot->has('some.random.key'); // false



$dot->define('leads.emails'); // Sets to an array
$hits = $dot->define('stats.mobile.hits', 0);
$dot->define('stats.console.hits', function () {
  // This function is called if this key is not set yet
  return 0;
});



// Merge into whole data array
$dot->merge([
  'stats' => [
    'web' => [
      'hits' => 123, 
      'leads' => 321
    ]
  ]
]);

$dot->get();

/* Returns:
Array
(
  [stats] => Array
    (
      [web] => Array
        (
          [hits] => 123
          [last_updated] => DateTime Object
            (
              [date] => 2017-07-25 13:34:49.000000
              [timezone_type] => 3
              [timezone] => America/Los_Angeles
            )

          [allow_tracking] => 1
          [leads] => 321
        )

      [mobile] => Array
        (
        )

    )
)
*/

// Merge array into a dot path
$dot->merge('stats.mobile', [
  'issues' => 33
]);

// Merge using function
$dot->merge('stats.mobile', function ($mobile) {
  return ['updated' => new DateTime];
});

// Merge into whole data array with function
$dot->merge(function ($data) {
  return ['new' => 123];
});