PHP Classes

PHP Dot Notation Array Access: Access array elements using path strings with dots

Recommend this page to a friend!
  Info   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 94 This week: 1All time: 9,860 This week: 571Up
Version License PHP version Categories
php-dot-array 1.0.2Custom (specified...7PHP 5, Data types
Description 

Author

This package can be used to access array elements using path strings with dots.

It can perform several types of operations using strings to refer to indexes of elements in arrays that can have one or more dimensions.

The class provides functions that can perform operations like setting, getting, deleting array element values, check if an element exists with a given path, return array element count, etc..

Picture of Moamen Eltouny
  Performance   Level  
Name: Moamen Eltouny <contact>
Classes: 36 packages by
Country: Egypt Egypt
Age: 31
All time rank: 259926 in Egypt Egypt
Week rank: 103 Up1 in Egypt Egypt Up
Innovation award
Innovation award
Nominee: 20x

Details

<p align="center"><a href="https://pharaonic.io" target="_blank"><img src="https://raw.githubusercontent.com/Pharaonic/logos/main/php/dot-array.jpg" width="470"></a></p>

<p align="center"> <a href="https://github.com/Pharaonic/php-dot-array" target="_blank"><img src="http://img.shields.io/badge/source-pharaonic/php--dot--array-blue.svg?style=flat-square" alt="Source"></a> <a href="https://packagist.org/packages/pharaonic/php-dot-array" target="_blank"><img src="https://img.shields.io/packagist/v/pharaonic/php-dot-array?style=flat-square" alt="Packagist Version"></a><br> <img src="https://img.shields.io/packagist/dt/pharaonic/php-dot-array?style=flat-square" alt="Packagist Downloads"> <img src="http://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square" alt="Source"> </p>

[PHP] DotArray - Accessing arrays using 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 pharaonic/php-dot-array

Usage

Create a new DotArray object:

$dot = new \Pharaonic\DotArray\DotArray;

// With existing array
$dot = new \Pharaonic\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:

<a name="set"></a>

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());

<a name="get"></a>

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

<a name="tojson"></a>

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();

<a name="all"></a>

all()

Returns all the stored items as an array:

$values = $dot->all();

<a name="delete"></a>

delete()

Deletes the given key:

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

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

<a name="clear"></a>

clear()

Deletes all the stored items:

$dot->clear();

<a name="has"></a>

has()

Checks if a given key exists (returns boolean):

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

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

<a name="count"></a>

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

<a name="isempty"></a>

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();

<a name="setarray"></a>

setArray()

Replaces all items in DotArray object with a given array:

$dot->setArray($array);

<a name="setreference"></a>

setReference()

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

$dot->setReference($array);

License

MIT license


  Files folder image Files  
File Role Description
Files folder imagesrc (2 files)
Files folder imagetests (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file composer.lock Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
  Plain text file DotArray.php Class Class source
  Accessible without login Plain text file helper.php Aux. Auxiliary script

  Files folder image Files  /  tests  
File Role Description
  Plain text file DotArrayTest.php Class Class source
  Plain text file HelpersTest.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:94
This week:1
All time:9,860
This week:571Up