diff --git a/readme.md b/readme.md index 2190791..03d9c2d 100644 --- a/readme.md +++ b/readme.md @@ -1,13 +1,13 @@ Ary === -Are you tired from casting objects and arrays to each other? Don't do that anymore! Ary is a light class/function that makes accessing array items more convenient. + +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. 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!) 3. You can specify a default value for missing indexes. - -* The class (`Ary()`) requires PHP 5.4 or newer. -* The helper function (`ary()`) requires PHP 5.6 or newer. +4. You can set/get a value within a deeply nested array using "dot" notation. Examples -------- @@ -17,30 +17,42 @@ $ary = new Ary(); // or simply $ary = ary(); -//setting and getting array items. - -$ary = ary(2, 4, 6, 8); //or +//Initialization +$ary = ary(2, 4, 6, 8); // or $ary = ary([2, 4, 6, 8]); - $ary = ary(['x' => 'foo', 'y' => 'bar']); -$foo = $ary->x; //or -$foo = $ary['x']; - -$missed = $ary->get('missed', 'Default value'); +//Assignment; $ary->newItem=20; //or $ary['newItem']=20; -count($ary); //returns 3 -$ary->all(); // returns simple php array; -~~~~~~ +//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 +count($ary); //returns 3 +unset($ary[0]); + +// deep assignment/retrieval +$ary = ary(['products' => ['desk' => ['price' => 100]]]); +$value = $ary['products.desk.price']; //100 +$ary['production.table.weight']=200; + +~~~~~~ Installation ============ +Simply, `include '__DIR__.'/src/helper.php';` +or using Composer: + composer require salarmehr/ary +* The class (`Ary()`) requires PHP 5.4 or newer. +* The helper function (`ary()`) requires PHP 5.6 or newer. Licence ======= diff --git a/src/Ary.php b/src/Ary.php index db50e97..71df75e 100644 --- a/src/Ary.php +++ b/src/Ary.php @@ -97,7 +97,16 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable if ($this->offsetExists($key)) { return $this->items[$key]; } - return $default; + + $array = $this->items; + foreach (explode('.', $key) as $segment) { + if (!is_array($array) || !array_key_exists($segment, $array)) { + return $default; + } + + $array = $array[$segment]; + } + return $array; } /** @@ -122,10 +131,28 @@ class Ary implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable { if (is_null($key)) { $this->items[] = $value; + return; } - else { - $this->items[$key] = $value; + +// $this->items[$key] = $value; + + $keys = explode('.', $key); + $array =& $this->items; + while (count($keys) > 1) { + $key = array_shift($keys); + + // If the key doesn't exist at this depth, we will just create an empty array + // to hold the next value, allowing us to create the arrays to hold final + // values at the correct depth. Then we'll keep digging into the array. + if (!isset($array[$key]) || !is_array($array[$key])) { + $array[$key] = []; + } + + $array = &$array[$key]; } + + $array[array_shift($keys)] = $value; + return; } /** diff --git a/test/Test.php b/test/Test.php index f79145b..5a04d72 100644 --- a/test/Test.php +++ b/test/Test.php @@ -61,6 +61,9 @@ class Test extends PHPUnit_Framework_TestCase { $ary = new Ary(); $this->assertEquals($ary[0], null); + + $ary = new Ary(['x' => ['xx' => ['m' => 'xxx']]]); + $this->assertEquals($ary->get('x.xx.m'), 'xxx'); }