2015-10-01 08:23:42 +03:30
|
|
|
Ary
|
|
|
|
===
|
2015-11-13 08:13:11 +03:30
|
|
|
|
|
|
|
Are you tired from casting objects and arrays to each other or are bored using is `isset`? Don't do those anymore!
|
|
|
|
Ary is a light class/function that makes accessing array items more convenient.
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-10-06 12:29:33 +03:30
|
|
|
1. You can access array items using `->` or `['']` syntax.
|
|
|
|
2. You will get `null` if an index does not exists (instead of a nasty notification!)
|
2015-10-17 19:17:25 +03:30
|
|
|
3. You can specify a default value for missing indexes.
|
2015-11-13 08:13:11 +03:30
|
|
|
4. You can set/get a value within a deeply nested array using "dot" notation.
|
2015-11-13 08:40:29 +03:30
|
|
|
5. A bunch of really handy method: `merge`,`only`,`search`,`toObject`, ...
|
2015-10-01 08:35:39 +03:30
|
|
|
|
2015-11-06 19:09:16 +03:30
|
|
|
Examples
|
|
|
|
--------
|
2015-10-01 08:23:42 +03:30
|
|
|
~~~~~
|
2015-11-13 08:40:29 +03:30
|
|
|
// Instantiation
|
2015-10-01 08:26:26 +03:30
|
|
|
$ary = new Ary();
|
2015-10-01 08:23:42 +03:30
|
|
|
// or simply
|
2015-10-01 08:26:26 +03:30
|
|
|
$ary = ary();
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-11-13 08:13:11 +03:30
|
|
|
//Initialization
|
|
|
|
$ary = ary(2, 4, 6, 8); // or
|
2015-10-06 12:29:33 +03:30
|
|
|
$ary = ary([2, 4, 6, 8]);
|
2015-10-01 08:26:26 +03:30
|
|
|
$ary = ary(['x' => 'foo', 'y' => 'bar']);
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-11-13 08:13:11 +03:30
|
|
|
//Assignment;
|
2015-10-01 08:32:48 +03:30
|
|
|
$ary->newItem=20; //or
|
|
|
|
$ary['newItem']=20;
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-11-13 08:13:11 +03:30
|
|
|
//Retrieval
|
|
|
|
$foo = $ary->x; //or
|
|
|
|
$foo = $ary['x'];
|
|
|
|
$missed = $ary->get('missed', 'Default value');
|
|
|
|
$ary->all(); // returns the simple php array;
|
|
|
|
|
|
|
|
// behave similar to regular arrays
|
2015-10-01 08:26:26 +03:30
|
|
|
count($ary); //returns 3
|
2015-11-13 08:13:11 +03:30
|
|
|
unset($ary[0]);
|
2015-11-13 08:40:29 +03:30
|
|
|
json_encode($ary);
|
2015-10-17 19:17:25 +03:30
|
|
|
|
2015-11-13 08:13:11 +03:30
|
|
|
// deep assignment/retrieval
|
|
|
|
$ary = ary(['products' => ['desk' => ['price' => 100]]]);
|
|
|
|
$value = $ary['products.desk.price']; //100
|
|
|
|
$ary['production.table.weight']=200;
|
|
|
|
|
|
|
|
~~~~~~
|
2015-10-17 19:17:25 +03:30
|
|
|
|
|
|
|
Installation
|
|
|
|
============
|
2015-11-13 08:13:11 +03:30
|
|
|
Simply, `include '__DIR__.'/src/helper.php';`
|
2015-10-17 19:17:25 +03:30
|
|
|
|
2015-11-13 08:13:11 +03:30
|
|
|
or using Composer:
|
|
|
|
|
2015-10-17 19:17:25 +03:30
|
|
|
composer require salarmehr/ary
|
|
|
|
|
2015-11-13 08:13:11 +03:30
|
|
|
* The class (`Ary()`) requires PHP 5.4 or newer.
|
|
|
|
* The helper function (`ary()`) requires PHP 5.6 or newer.
|
2015-10-17 19:17:25 +03:30
|
|
|
|
2015-11-06 19:09:16 +03:30
|
|
|
Licence
|
|
|
|
=======
|
|
|
|
MIT
|