寻找在关联数组最高价值 [英] Finding Highest Value in Associative Array
本文介绍了寻找在关联数组最高价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个比较简单的方法来找到自己的关联数组每个按键的最高价值?
阵列( 0 =>阵列(
avgtime=> 19.75,
反弹=> 3,
浏览量=> 14,
游客=> 4
) 1 =>阵列(
avgtime=> 1.125,
反弹=> 7,
浏览量=> 9,
游客=> 8
) 2 =>阵列(
avgtime=> 111,
反弹=> 18,
浏览量=> 32,
游客=> 20
) 3 =>阵列(
avgtime=> 6.9375,
反弹=> 14,
浏览量=> 18,
游客=> 10
) 4 =>阵列(
avgtime=> 191,
反弹=> 11,
浏览量=> 57,
游客=> 24
)
);
我要创建一个数组保存每个键的最高值,因此最终结果将是。
阵列( avgtime=> 191,
反弹=> 18,
浏览量=> 57,
游客=> 24
);
解决方案
遍历数组,并存储最大值为每个键
$ TEMP =阵列();
的foreach($为$项数据){
的foreach($项目作为重点$ = GT; $值){
$临时[$关键] = MAX(
使用isset($临时[$关键])? $临时[$关键]:$值,
$值);
}
}
Is there a relatively easy way to find the highest values for each of the keys in my associative arrays?
Array(
0 => Array(
"avgtime" => 19.75,
"bounces" => 3,
"pageviews" => 14,
"visitors" => 4
)
1 => Array(
"avgtime" => 1.125,
"bounces" => 7,
"pageviews" => 9,
"visitors" => 8
)
2 => Array(
"avgtime" => 111,
"bounces" => 18,
"pageviews" => 32,
"visitors" => 20
)
3 => Array(
"avgtime" => 6.9375,
"bounces" => 14,
"pageviews" => 18,
"visitors" => 10
)
4 => Array(
"avgtime" => 191,
"bounces" => 11,
"pageviews" => 57,
"visitors" => 24
)
);
I want to create one array that holds the highest value for each key so the end result would be.
Array(
"avgtime" => 191,
"bounces" => 18,
"pageviews" => 57,
"visitors" => 24
);
解决方案
Iterate over the Array and store the max values for each key
$temp = array();
foreach ($data as $item) {
foreach ($item as $key => $value) {
$temp[$key] = max(
isset($temp[$key]) ? $temp[$key] : $value,
$value);
}
}
这篇关于寻找在关联数组最高价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文