Wanted: PHP built-in functionality for dynamically accessing array elements at arbitrary nesting depth

In general I’ve been pretty happy with the array implementation in PHP, finding them pretty versatile, flexible, and easy to use. I like that you can just mix integer and string keys, associative arrays are ordered, writing to a nonexistent nested array auto-vivifies it, reading a nonexistent key / index just evaluates to NULL and doesn’t generate an error, etc. However, I do see room for improvement.

(If you want to experiment with code examples in this post, see the reference code.)

The subject of this article is that for a long time I’ve wished for built-in functionality in PHP for dynamically accessing array elements at arbitrary nesting level. I’m talking about what are called multi-dimensional [possibly associative] arrays in PHP, and arrays / hashes / maps in other languages. In any case, I’m talking about tree structures / hierarchies built by nesting those data types. For example, consider this array:

Continue reading