I receive a large multi-dimensional array from an API. I save the array into a class property and then I use a magic getter to disguise the array and have end users use keys as properties ($class->key ==> $class->data['key']).
Now the array I am dealing with is very much multi-dimensional and I need keys on different "levels" or layers. Imagine the array has two branches with multiple layers.
The keys I am looking for are usually the top level.
I will try to illustrate this:
Array
baseKey
key1
value1
key2
value2
key3
subKey1
value3
subKey2
value4
key4
value5
key5
subKey3
value6
key6
subKey4
subSubKey1
value7
I am looking for a function that I can give any key to, including baseKey, key, subKey and subSubKey and that will return the respective value for the first key that matches.
So as an example:
awesomeFindByKey('key2')
==> value2
awesomeFindByKey('subKey4')
==> array('subSubKey1' => 'value7')
awesomeFindByKey('key6')
==> array('subKey4' => array('subSubKey1' => 'value7'))
Is there a function for this or will I have to write one myself? Does anybody maybe have a function that does this?
I hope it was understandable. Thanks.