Set value of an array using xpath notation

This is a continuation to my previous article on how to get values of multi-dimensional arrays using xpath notation.

This time I will show you how to set a value of an array using the same xpath notation (root/branch/leaf).

Get values of multi-dimensional arrays using xpath notation

Sometimes we have a value burried deep within a multi-dimensional array. In order to access it we usually end up doing something like this:

  1. $value = $array['a']['b']['c'];

hoping that all the keys are set. However, we have a problem if 'b' is not set or any other of the "path" keys are missing.

Wouldn't it be nice if we could get the value using something similar to xpath, e.g. "a/b/c" and specify the default value if the chain is broken? I thought it would and wrote this little function or/and extension to ArrayObject.

Showing 1 - 2 of 2 results