Oliverde8's blue Website

On going

PHP Associative Array Simplified

Start Date :
17/02/2017
Link
Github
Update Date :
17/02/2017

This library allow you to get data from an associative array with ease. This can be used many places even for getting information from SESSION or POST & GET values.

Typically when we try and get information from a an array of which we don't know the content we need to write a lot of conditions :

$array = ['a' => ['b' => 2, 'c' => 3]];
if (isset($array['a'])) {
    $valB = isset($array['a']['b']) ? $array['a']['b'] : "default";
    $valB = isset($array['a']['c']) ? $array['a']['c'] : "default";
}

After a certain while we write a lot conditions and the array is very nested it becomes difficult to understand. This library makes it much easier to write code of this type & makes it easier to read and understand. The code above is simply replaced by :

$object = new AssociativeArray( ['a' => ['b' => 2, 'c' => 3]]);
$valB = $object->get('a/b', 'default');
$valB = $object->get('a/c', 'default');