user3448192
user3448192

Reputation: 3

Getting value from an array of keys for a multidimensional array

In PHP, I have a search function that will perform a stripos search in a multidimensional array (that is very deep) and if the value is found, it will return an array with the keys. For example, the return array will look like this:

$my_results = array(0 => 'text', 1 => '17', 2 => 'tspan', 3 => '8', 4 => 'red');

The values are the keys from the multidimensional array.

How do I programmatically reference these keys to retrieve the value? (Also, the depth of the array may change and is not known).

Hardcoding like this works:

echo $my_array['text'][17]['tspan'][8]['red']; // displays the value

but, I would like to retrieve the value from the keys given in the array

Upvotes: 0

Views: 62

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

You mean something like:

$arrayNode = $myDeeplyNestedArray;
foreach($myArrayKeys as $key) {
    $arrayNode = $arrayNode[$key];
}

Upvotes: 1

Related Questions