Recommend this page to a friend! |
Download |
Info | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
Not yet rated by the users | Total: 94 | All time: 9,889 This week: 206 |
Version | License | PHP version | Categories | |||
php-dot-array 1.0.2 | Custom (specified... | 7 | PHP 5, Data types |
Description | Author | |
This package can be used to access array elements using path strings with dots. |
<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>
//Get all users names with DotArray
$names = $dot->get('users.*.name');
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 the latest version using Composer:
$ composer require pharaonic/php-dot-array
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);
DotArray has the following methods:
<a name="set"></a>
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>
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>
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>
Returns all the stored items as an array:
$values = $dot->all();
<a name="delete"></a>
Deletes the given key:
$dot->delete('users.*.name');
// ArrayAccess
unset($dot['users.*.name']);
<a name="clear"></a>
Deletes all the stored items:
$dot->clear();
<a name="has"></a>
Checks if a given key exists (returns boolean):
$dot->has('users.raggi.name');
// ArrayAccess
isset($dot['users.raggi.name']);
<a name="count"></a>
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>
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>
Replaces all items in DotArray object with a given array:
$dot->setArray($array);
<a name="setreference"></a>
Replaces all items in Dot object with a given array as a reference:
$dot->setReference($array);
Files (9) |
File | Role | Description | ||
---|---|---|---|---|
src (2 files) | ||||
tests (2 files) | ||||
composer.json | Data | Auxiliary data | ||
composer.lock | Data | Auxiliary data | ||
LICENSE | Lic. | License text | ||
phpunit.xml | Data | Auxiliary data | ||
README.md | Doc. | Documentation |
Files (9) | / | src |
File | Role | Description |
---|---|---|
DotArray.php | Class | Class source |
helper.php | Aux. | Auxiliary script |
Files (9) | / | tests |
File | Role | Description |
---|---|---|
DotArrayTest.php | Class | Class source |
HelpersTest.php | Class | Class source |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Unique User Downloads | Download Rankings | |||||||||||||||
100% |
|
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.