我有一系列关于赛狗的统计数据。我需要通过添加另一个元素来突出显示每个元素中的最大值和最小值。让我解释一下。
数组示例
Array
(
[1] => Array
(
[fast_calc_7] => 31.06
[av_calc_7] => 25.03
)
[2] => Array
(
[fast_calc_7] => 16.74
[av_calc_7] => 18.06
)
[3] => Array
(
[fast_calc_7] => 30.93
[av_calc_7] => 31.06
)
[4] => Array
(
[fast_calc_7] => 29.01
[av_calc_7] => 25.08
)
[5] => Array
(
[fast_calc_7] => 30.72
[av_calc_7] => 31.02
)
[6] => Array
(
[fast_calc_7] => 31.16
[av_calc_7] => 36.02
)
)
结果数组的示例
我需要比较这些值并添加另一个元素,指定它是否是最低值的最高值。例如,它应该生成一个如下的数组。
Array
(
[1] => Array
(
[fast_calc_7] => 31.06
[av_calc_7] => 25.03
)
[2] => Array
(
[fast_calc_7] => 16.74
[fast_calc_7_lowest] => TRUE
[av_calc_7] => 18.06
[av_calc_7_lowest] => TRUE
)
[3] => Array
(
[fast_calc_7] => 30.93
[av_calc_7] => 37.06
[av_calc_7_highest] => TRUE
)
[4] => Array
(
[fast_calc_7] => 29.01
[av_calc_7] => 25.08
)
[5] => Array
(
[fast_calc_7] => 30.72
[av_calc_7] => 31.02
)
[6] => Array
(
[fast_calc_7] => 31.16
[fast_calc_7_highest] => TRUE
[av_calc_7] => 36.02
)
)
我确信有一种非常简单的方法可以做到这一点。我越需要遍历数组,我的应用程序就越慢!
谢谢你们。
编辑:如何获取数据
答案 0 :(得分:1)
没有多少。您需要循环遍历数组,为每个属性维护具有高/低元素索引的变量。循环之后,访问这些元素并添加额外的数据。
$lowAverage = $highAverage = array('index' => null, 'value' => 0);
$lowFast = $highFast = array('index' => null, 'value' => 0);
foreach($dogs as $index => $data) {
if($lowAverage['index'] == null || $data['av_calc_7'] < $lowAverage['value']) {
$lowAverage = array('index' => $index, 'value' => $data['av_calc_7']);
}
if($highAverage['index'] == null || $data['av_calc_7'] > $highAverage['value']) {
$highAverage = array('index' => $index, 'value' => $data['av_calc_7']);
}
// same for $lowFast, $highFast
}
if($lowAverage['index'] != null) {
$dogs[$lowAverage['index']]['av_calc_7_lowest'] = true;
}
// same for the other 3 values