I've tried to adjust many similar solutions that I've found here on stack by none worked for me. Could someone please help me?
This multidimensional array is dynamically generated (contains 55 keys total).
There is variable $age which users enter, and $age
correspondents to second array key, in this example [15]. By this key $age
I have to find out parent key val $key
.
In order to echo final value I need that top level array key ($key
). Here is what echo would look like:
$val = $array[$key][$age]["stadij1"]["20-40"];
echo $val;
How do I target top level parent array key([0]) of key [15]?
Array
(
[0] => Array
(
[15] => Array
(
[stadij1] => Array
(
[0-20] => 0
[20-40] => 61
[40-80] => 38
[80-120] => 30
[120-xx] => 27
)
[stadij2] => Array
(
[0-20] => 0
[20-40] => 50
[40-80] => 32
[80-120] => 27
[120-xx] => 24
)...
)...
//my try
$key = array_search($age,$array); //problem is that it returns only first element
$val = $array[$key][$age]["stadij1"]["20-40"];
echo $val;
答案 0 :(得分:-1)
If I understood correctly, you mean something like:
function findKey($array, $age)
{
foreach ($array as $parentIndex => $parentValue) {
foreach ($row as $index => $value) {
if ($index === $age) {
return $parentIndex;
}
}
}
throw new Exception('key not found');
}