[英]find highest value in associative array
I've got an array which looks like this: 我有一个看起来像这样的数组:
array(10) {
[0]=>
array(4) {
[0]=>
array(5) {
...
}
[1]=>
array(5) {
...
}
[2]=>
array(5) {
...
}
["opt"]=>
float(0.5)
}
[1]=>
array(4) {
[0]=>
array(5) {
...
}
[1]=>
array(5) {
...
}
[2]=>
array(5) {
...
}
["opt"]=>
float(1)
}
.....
}
I want to find out the Key of the Array of the first layer, which has the highest value at the key 'opt'. 我想找出第一层数组的键,键“ opt”处的值最高。 I hope you got what i mean.
我希望你明白我的意思。 Sorry for that sentence, but it was kind of hard to explain!
抱歉,这句话很难解释! ;)
;)
EDIT: What I've thought of was the use of max() creating all the entries with the use of foreach. 编辑:我想到的是使用max()使用foreach创建所有条目。 I wasnt sure if this would be the best way.
我不确定这是否是最好的方法。
You can use the max() function 您可以使用max()函数
if your array have a constant length "n" 如果您的数组的长度为常数“ n”
$maxValue=max($array[0]["opt"],$array[1]["opt"], ... $array[n]["opt"]);
else 其他
$i=0;
foreach($array as $value) {
$opts[$i]=$value["opt"];
$i++;
}
$maxValue=max($opts);
What about max()
+ array_map()
? 那么
max()
+ array_map()
呢?
$max = max(
array_map(function (array $layer) {
return (float)$layer['opt'];
}, $layers)
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.