Selvin Ortiz

Traversing Arrays Using Dot Notation

December 6, 2014 by

Cover Image for Traversing Arrays Using Dot Notation

Dot notation allows us to traverse an array in a very elegant way, it is specially useful when working with deeply nested sets.

Let me show you what I mean by "dot notation".

The array to traverse #

Here is a somewhat deeply nested array to work with…

$developer = array(
    'name'   => 'Brad Bell',
    'mood'   => 'Angry',
    'family' => array(
        'spouse'  => array(
            'name' => 'Brandon Kelly'
        'brother' => array(
            'name' => 'That European Guy'

Pretty contrived example but it will do: )

Dot notated value getter function #

This simple getValueByKey() function is one way to implement dot notation support and I opted to keep it simple for the purpose of this example.

By simple I mean, I’m not using multibyte string functions or using recursion to optimize execution or anything else.

function getValueByKey($key, array $data, $default = null)
    // @assert $key is a non-empty string
    // @assert $data is a loopable array
    // @otherwise return $default value
    if (!is_string($key) || empty($key) || !count($data))
        return $default;

    // @assert $key contains a dot notated string
    if (strpos($key, '.') !== false)
        $keys = explode('.', $key);

        foreach ($keys as $innerKey)
            // @assert $data[$innerKey] is available to continue
            // @otherwise return $default value
            if (!array_key_exists($innerKey, $data))
                return $default;

            $data = $data[$innerKey];

        return $data;

    // @fallback returning value of $key in $data or $default value
    return array_key_exists($key, $data) ? $data[$key] : $default;

If you like optimizing this kind of stuff like I do, feel free to check out the gist and go to town:)

Using dot notation #

We can now access deeply nested keys with the ability to provide a sensible default value if the provided key is not found.

getValueByKey('', $developer, 'Matt Stauffer');
// Brandon Kelly

getValueByKey('family.spouse.nonExistent', $developer, 'Matt Stauffer');
// Matt Stauffer

getValueByKey('', $developer);
// Array
// (
//    [name] => That European Guy
// )

The equivalent (or at least one way of doing it) in vanilla PHP would be something like this…

echo    isset($developer['family']['spouse']['name'])
        'Matt Stauffer'
// Brandon Kelly

Now you know dot notation #

The next time you’re working on a Craft CMS plugin and need to access deeply nested variables inside $_POST or $_GET, you can use something like the getValueByKey() function.

Better yet, you can use the implementation Craft itself provides which deals with UTF8 and multibyte string issues.


The Craft guys are good at giving us the tools we need that way, one reason I love their product for sure.

That’s about it, go traverse some deeply nested arrays while you sip on hot cocoa: )

Categorized As