1

I'm not sure if there is such a function, but I'd expect it to do the following: get_array_value($array, $chain); where $array is the array to search for the value, and $chain is an array that contains information which value to retrieve.

Example usage: if $chain = array('key1', 'key2', 'key3');, then the function should return $array['key1']['key2']['key3'];

Is there anything similar out there already and if not, how could I achieve this?

Thanks in advance!

UPDATE:

Huh, the intended result should be a single value, not an array. So I could use it like echo get_array_value($array, $chain);

2 Answers 2

5
$cloneArray = $array;  // clone it for future usage
foreach($chain as $value)
{
   $cloneArray =  $cloneArray[$value];
}

var_dump($cloneArray);
Sign up to request clarification or add additional context in comments.

Comments

3
function resolve ($array, $chain) {
    return empty($chain) 
        ? $array;
        :resolve($array[array_shift($chain)], $chain);
}

Thats the very short form. You must verify, that all keys, that you want to resolve, exists (and such).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.