2015-10-01 08:23:42 +03:30
|
|
|
Ary
|
|
|
|
===
|
2015-10-01 08:26:26 +03:30
|
|
|
Are you tired from casting objects and array to each other? Ary is a light class/function that makes accessing array items more convenient.
|
2015-10-01 08:23:42 +03:30
|
|
|
|
|
|
|
1. You can access array items using `->` or `[]` syntax.
|
|
|
|
2. You will get `null` if an index does not exists.
|
|
|
|
3. You can specify a default value for a missing index.
|
|
|
|
|
2015-10-01 08:35:39 +03:30
|
|
|
* The class (`Ary()`) requires PHP 5.4 or newer.
|
|
|
|
* The helper function (`ary()`) requires PHP 5.6 or newer.
|
|
|
|
|
2015-10-01 08:23:42 +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-10-01 08:32:48 +03:30
|
|
|
//setting and getting array items.
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-10-01 08:26:26 +03:30
|
|
|
$ary = ary(2, 4, 6, 8); //or
|
|
|
|
$ary = ary([2, 4, 6, 8]); //or
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-10-01 08:26:26 +03:30
|
|
|
$ary = ary(['x' => 'foo', 'y' => 'bar']);
|
|
|
|
$foo = $ary->foo; //or
|
|
|
|
$foo = $ary['foo'];
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-10-01 08:26:26 +03:30
|
|
|
$missed = $ary->get('missed', 'Default value');
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-10-01 08:32:48 +03:30
|
|
|
$ary->newItem=20; //or
|
|
|
|
$ary['newItem']=20;
|
2015-10-01 08:23:42 +03:30
|
|
|
|
2015-10-01 08:26:26 +03:30
|
|
|
count($ary); //returns 3
|
2015-10-01 08:35:39 +03:30
|
|
|
$ary->all(); // returns simple php array;
|