从多维数组的键数组中获取值

时间:2022-11-19 21:28:48

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:

在PHP中,我有一个搜索函数,它将在多维数组(非常深)中执行stripos搜索,如果找到该值,它将返回带有键的数组。例如,返回数组将如下所示:

$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 个解决方案

#1


1  

You mean something like:

你的意思是:

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

#1


1  

You mean something like:

你的意思是:

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