raggitech/php-dot-array

This package is abandoned and no longer maintained. The author suggests using the pharaonic/php-dot-array package instead.

Access array data quickly/easily using dot-notation and asterisk.

This package has no released version yet, and little information is available.


README

Latest Stable Version Total Downloads License CI-Build

DotArray provides a quick and easy access to arrays of data with dot-notation and asterisk.

//Get all users names with DotArray
$names = $dot->get('users.*.name');

Examples

Traditional way

$array['users']['raggi']['name'] = 'Moamen Eltouny';

echo $array['users']['raggi']['name']; // Moamen Eltouny

DotArray way (with DotArray Object)

$dot = dot(); // Creating DotArray Object
$dot->set('users.raggi.name', 'Moamen Eltouny');

// Getting [DotArray way]
echo $dot->get('users.raggi.name');

// OR Getting [ArrayAccess way]
echo $dot['users.raggi.name'];

Install

Install the latest version using Composer:

$ composer require raggitech/php-dot-array

Usage

Create a new DotArray object:

$dot = new \RaggiTech\DotArray\DotArray;

// With existing array
$dot = new \RaggiTech\DotArray\DotArray($array);

OR You can use a helper function to create the object:

$dot = dot();

// With existing array
$dot = dot($array);

Methods

DotArray has the following methods:

set()

Sets a given key / value pair:

$dot->set('users.raggi.created_at', date('r', time()));

// ArrayAccess
$dot['users.raggi.created_at'] = date('r', time());

get()

Returns the value of a given key:

print_r($dot->get('users.*.name'));

// ArrayAccess
print_r($dot['users.*.name']);

Returns a given default value, if the given key doesn't exist:

print_r($dot->get('users.*.name', 'Raggi'));

toJson()

Returns the value of a given key (like get() method) as JSON:

echo $dot->toJson('users');

Returns all the stored items (like get() method) as JSON:

echo $dot->toJson();

all()

Returns all the stored items as an array:

$values = $dot->all();

delete()

Deletes the given key:

$dot->delete('users.*.name');

// ArrayAccess
unset($dot['users.*.name']);

clear()

Deletes all the stored items:

$dot->clear();

has()

Checks if a given key exists (returns boolean):

$dot->has('users.raggi.name');

// ArrayAccess
isset($dot['users.raggi.name']);

count()

Returns the number of the root Items:

$dot->count();

// Or use count() function [Countable Way]
count($dot);

Returns the number of items in a given key:

$dot->count('users');

isEmpty()

Checks if a given key is empty (returns boolean):

$dot->isEmpty('users.raggi.name');

// ArrayAccess
empty($dot['users.raggi.name']);

Checks the whole DotArray object:

$dot->isEmpty();

setArray()

Replaces all items in DotArray object with a given array:

$dot->setArray($array);

setReference()

Replaces all items in Dot object with a given array as a reference:

$dot->setReference($array);

License

MIT license