<?php
$array = array(
"FL_1" => array(
"MIC_1" => array(
"SP_4" => 7
),
"MIC_13" => array(
"SP_16" => 4
),
"MIC_6" => array(
"SP_74" => 4
)
),
"FL_2" => array(
"MIC_1" => array(
"SP_5" => 4
),
"MIC_13" => array(
"SP_17" => 4
),
"MIC_6" => array(
"SP_75" => 4
)
),
"FL_3" => array(
"MIC_1" => array(
"SP_5" => 89
),
"MIC_13" => array(
"SP_18" => 1
),
"MIC_6" => array(
"SP_78" => 21
)
)
);
// find the minimum SP_X value and its key
$min_sp = PHP_INT_MAX;
$min_key = '';
array_walk_recursive($array, function ($v, $k) use (&$min_sp, &$min_key) {
if ($v < $min_sp) {
$min_sp = $v;
$min_key = $k;
}
});
echo "$min_key => $min_sp\n";
// find the MIC_X key corresponding to the min SP_X value
$mic_key = '';
foreach ($array as $fl) {
foreach ($fl as $mic => $sp) {
if (isset($sp[$min_key]) && $sp[$min_key] == $min_sp) {
$mic_key = $mic;
break 2;
}
}
}
echo "$mic_key\n";
// filter the array to get all the MIC_X values
$out = array_map(function ($fl) use ($mic_key) {
return array_filter($fl, function ($mic) use ($mic_key) {
return $mic == $mic_key;
}, ARRAY_FILTER_USE_KEY);
}, $array);
print_r($out);
preferences:
24.84 ms | 407 KiB | 5 Q