2017 © Pedro Peláez
 

library dot

PHP dot notation array access

image

prowebcraft/dot

PHP dot notation array access

  • Thursday, February 15, 2018
  • by prowebcraft
  • Repository
  • 1 Watchers
  • 1 Stars
  • 77 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 10 % Grown

The README.md

Dot - PHP dot notation array access

Based on adbario/php-dot-notation package, (*1)

Easy access to multidimensional arrays with dot notation. With dot notation, your code is cleaner and handling deeper arrays is super easy., (*2)

This class implements PHP's ArrayAccess class, so Dot object can also be used the same way as normal arrays with additional dot notation., (*3)

With Dot you can change this:, (*4)

echo $data['info']['home']['address'];

to this:, (*5)

echo $data->get('info.home.address');

or even this:, (*6)

echo $data['info.home.address'];

Installation

Via composer:, (*7)

composer require prowebcraft/dot

Or just copy the class file Dot.php and handle namespace yourself., (*8)

With Composer:

composer require prowebcraft/dot

Manual installation:

  1. Download the latest release
  2. Extract the files into your project
  3. require_once '/path/to/dot/src/Dot.php';

Usage

This array will be used as a reference on this guide:, (*9)

$array = [
    'user' => [
        'firstname' => 'John',
        'lastname'  => 'Smith'
    ],
    'info' => [
        'kids' => [
            0 => 'Laura',
            1 => 'Chris',
            2 => 'Little Johnny'
        ],
        'home' => [
            'address' => 'Rocky Road 3'
        ]
    ]
];

Create a Dot object

To start with an empty array, just create a new Dot object:, (*10)

$data = new \Prowebcraft\Dot;

If you have an array already available, inject it to the Dot object:, (*11)

$data = new \Prowebcraft\Dot($array);

Set an array after creating the Dot object:, (*12)

$data->setArray($array);

Set an array as a reference, and all changes will be made directly to the original array:, (*13)

$data->setReference($array);

Set a value

Set i.e. a phone number in the 'home' array:, (*14)

$data->set('info.home.tel', '09-123-456-789');

// Array style
$data['info.home.tel'] = '09-123-456-789';

Set multiple values at once:, (*15)

$data->set([
    'user.haircolor'    => 'blue',
    'info.home.address' => 'Private Lane 1'
]);

If the value already exists, Dot will override it with a new value., (*16)

Get a value

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'];

Get all the stored values:, (*17)

$values = $data->all();
``

Get a value from a path and remove it:

```php
$address = $data->pull('home.address');

Get all the stored values and remove them:, (*18)

$values = $data->pull();

Add a value

$data->add('info.kids', 'Amy');

Multiple values at once:, (*19)

$data->add('info.kids', [
    'Ben', 'Claire'
]);

Check if a value exists

if ($data->has('info.home.address')) {
    // Do something...
}

// Array style
if (isset($data['info.home.address'])) {
    // Do something...
}

Delete a value

$data->delete('info.home.address');

// Array style
unset($data['info.home.address']);

Multiple values at once:, (*20)

$data->delete([
    'user.lastname', 'info.home.address'
]);

Clear values

Delete all the values from a path:, (*21)

$data->clear('info.home');

Clear multiple paths at once:, (*22)

$data->clear([
    'user', 'info.home'
]);

Clear all data:, (*23)

$data->clear();

Sort the values

You can sort the values of a given path or all the stored values., (*24)

Sort the values of a path:, (*25)

$kids = $data->sort('info.kids');

// Sort recursively
$info = $data->sort('info');

Sort all the values, (*26)

$sorted = $data->sort();

// Sort recursively
$sorted = $data->sort();

Magic methods

Magic methods can be used to handle single level data (without dot notation). These examples are not using the same data array as examples above., (*27)

Set a value:, (*28)

$data->name = 'John';

Get a value:, (*29)

echo $data->name;

Check if a value exists:, (*30)

if (isset($data->name)) {
    // Do something...
}

Delete a value:, (*31)

unset($data->name);

License

MIT license, (*32)

The Versions

15/02 2018

dev-master

9999999-dev https://github.com/prowebcraft/php-dot-notation

PHP dot notation array access

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Andrey Mistulov

php arrayaccess dotnotation

15/02 2018

0.1.0

0.1.0.0 https://github.com/prowebcraft/php-dot-notation

PHP dot notation array access

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

by Andrey Mistulov

php arrayaccess dotnotation