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

Wanted: PHP core function for dynamically performing double-quoted string variable interpolation

For a long time I’ve wished PHP had a built-in function for dynamically evaluating a string variable as a double-quoted string or HEREDOC, to perform variable interpolation (aka expansion) on-the-fly. It’s frequently useful to store a template in a string and then interpolate data into it later, pass the template to a function, etc. Example:

Continue reading