在 PHP 中转换一个 3 维数组

我有一个这样的数组:


array(3) {

    ["FL_1"] => array(3) {

        ["MIC_1"] => array(1) {

            ["SP_4"] => float(7)

        }

        ["MIC_13"] => array(1) {

            ["SP_16"] => float(4)

        }

        ["MIC_6"] => array(1) {

            ["SP_74"] => float(4)

        }

    }

    ["FL_2"] => array(2) {

        ["MIC_1"] => array(1) {

            ["SP_5"] => float(4)

        }

        ["MIC_13"] => array(1) {

            ["SP_17"] => float(4)

        }

        ["MIC_6"] > array(1) {

            ["SP_75"] => float(4)

        }

    }

    ["FL_3"] => array(2) {

        ["MIC_1"] => array(1) {

            ["SP_5"] => float(89)

        }

        ["MIC_13"] => array(1) {

            ["SP_18"] => float(1)

        }

        ["MIC_6"] > array(1) {

            ["SP_78"] => float(21)

        }

    }

}

对于每一个FL_X,我只需要保留一个MIC_X满足以下条件的:


1-MIC_X每个都需要相同FL_X

2- 这MIC_X需要具有尽可能低的SP_X值


从这个例子我需要得到以下数组


array(3) {

    ["FL_1"] => array(1) {

        ["MIC_13"] => array(1) {

            ["SP_16"] => float(4)

        }

    }

    ["FL_2"] => array(1) {

        ["MIC_13"] => array(1) {

            ["SP_17"] => float(6)

        }

    }

    ["FL_3"] => array(1) {

        ["MIC_13"] => array(1) {

            ["SP_18"] => float(1)

        }

    }

}

任何有关如何执行此操作的帮助将不胜感激。


慕尼黑8549860
浏览 59回答 1
1回答

拉莫斯之舞

这是一种可能的解决方案。它用于array_walk_recursive查找SP_X与最小值关联的键SP_X,然后遍历数组以查找MIC_X与该键和值关联的SP_X键,最后它使用array_mapandarray_filter仅从原始数组中提取那些MIC_X键值:// 找到最小的 SP_X 值和它的键$min_sp = PHP_INT_MAX;$min_key = '';array_walk_recursive($array, function ($v, $k) use (&$min_sp, &$min_key) {&nbsp; &nbsp; if ($v < $min_sp) {&nbsp; &nbsp; &nbsp; &nbsp; $min_sp = $v;&nbsp; &nbsp; &nbsp; &nbsp; $min_key = $k;&nbsp; &nbsp; }&nbsp;});// find the MIC_X key corresponding to the min SP_X value$mic_key = '';foreach ($array as $fl) {&nbsp; &nbsp; foreach ($fl as $mic => $sp) {&nbsp; &nbsp; &nbsp; &nbsp; if (isset($sp[$min_key]) && $sp[$min_key] == $min_sp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $mic_key = $mic;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break 2;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// filter the array to get all the MIC_X values$out = array_map(function ($fl) use ($mic_key) {&nbsp; &nbsp; return array_filter($fl, function ($mic) use ($mic_key) {&nbsp; &nbsp; &nbsp; &nbsp; return $mic == $mic_key;&nbsp; &nbsp; }, ARRAY_FILTER_USE_KEY);}, $array);print_r($out);输出:Array(&nbsp; &nbsp; [FL_1] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [MIC_13] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [SP_16] => 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [FL_2] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [MIC_13] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [SP_17] => 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [FL_3] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [MIC_13] => Array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [SP_18] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; ))3v4l.org 上的演示
打开App,查看更多内容
随时随地看视频慕课网APP