0

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

1 Answer 1

1

You mean something like:

$arrayNode = $myDeeplyNestedArray;
foreach($myArrayKeys as $key) {
    $arrayNode = $arrayNode[$key];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Mark for the reply. I assume the $myArrayKeys is the array of keys that I have received from my search function? And $myDeeplyNestedArray is the full multidimensional array that was originally searched?
That's right, I'd tried to make the variable names that I used meaningful
That worked! I learned something new today... I swear I tried different combinations but nothing seemed to work. THANKS!

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.