I want to get the highest value, the second highest value and the third highest value
我想要得到最高的值,第二高的值和第三高的值
For example, I have an array like:
例如,我有一个数组,比如:
$n = array(100,90,150,200,199,155,15,186);
$ n =数组(100、90150200199155、15186);
I know the method to get the max value and its index:
我知道求最大值的方法及其指标:
echo max($n); //200 $maxs = array_keys($n, max($n)); echo $maxs[0]; //3
回声马克斯($ n);//200 $maxs = array_keys($n, max($n));echo $马克斯[0];/ / 3
I want to get the top 3 values and their index like : value: 200, 199, 186 index:3,4,7
我想要得到前3个值以及它们的索引:value: 200,199,186 index:3,4,7
How can i get them?
我怎样才能得到它们?
5 个解决方案
#1
2
Try this:
试试这个:
$n = array(100,90,150,200,199,155,15,186);
rsort($n);
$top3 = array_slice($n, 0, 3);
echo 'Values: ';
foreach ($top3 as $key => $val) {
echo "$val\n";
}
echo '<br>';
echo 'Keys: ';
foreach ($top3 as $key => $val) {
echo "$key\n";
}
Output:
输出:
Values: 200 199 186
Keys: 0 1 2
#2
2
This should do the trick:
这应该能达到目的:
function maxNitems($array, $n = 3){
asort($array);
return array_slice(array_reverse($array, true),0,$n, true);
}
Use like:
使用:
maxNitems(array(100,90,150,200,199,155,15,186));
#3
0
You can achieve it by using arsort()
and array_keys()
functions:
可以通过使用arsort()和array_keys()函数实现:
-
arsort()
sorts an array in reverse order and maintains index association - arsort()对数组进行反向排序并保持索引关联
-
array_keys()
returns all the keys or a subset of the keys of an array - array_keys()返回数组的所有键或键的子集
Process array:
过程数组:
$n = array(100,90,150,200,199,155,15,186);
arsort($n);
$keys = array_keys($n);
Get top 3 values:
获得前三值:
echo $n[$keys[0]];
echo $n[$keys[1]];
echo $n[$keys[2]];
#4
0
$n = array(100,90,150,200,199,155,15,186);
arsort($n);
$x = 0;
while (++$x <= 3)
{
$key = key($n);
$value = current($n);
next($n);
echo "Key : " . $key . " Value : " . $value . '<br>' ;
}
#5
0
Easier I would think:
简单的我认为:
arsort($n);
$three = array_chunk($n, 3, true)[0];
//or
$three = array_slice($n, 0, 3, true);
#1
2
Try this:
试试这个:
$n = array(100,90,150,200,199,155,15,186);
rsort($n);
$top3 = array_slice($n, 0, 3);
echo 'Values: ';
foreach ($top3 as $key => $val) {
echo "$val\n";
}
echo '<br>';
echo 'Keys: ';
foreach ($top3 as $key => $val) {
echo "$key\n";
}
Output:
输出:
Values: 200 199 186
Keys: 0 1 2
#2
2
This should do the trick:
这应该能达到目的:
function maxNitems($array, $n = 3){
asort($array);
return array_slice(array_reverse($array, true),0,$n, true);
}
Use like:
使用:
maxNitems(array(100,90,150,200,199,155,15,186));
#3
0
You can achieve it by using arsort()
and array_keys()
functions:
可以通过使用arsort()和array_keys()函数实现:
-
arsort()
sorts an array in reverse order and maintains index association - arsort()对数组进行反向排序并保持索引关联
-
array_keys()
returns all the keys or a subset of the keys of an array - array_keys()返回数组的所有键或键的子集
Process array:
过程数组:
$n = array(100,90,150,200,199,155,15,186);
arsort($n);
$keys = array_keys($n);
Get top 3 values:
获得前三值:
echo $n[$keys[0]];
echo $n[$keys[1]];
echo $n[$keys[2]];
#4
0
$n = array(100,90,150,200,199,155,15,186);
arsort($n);
$x = 0;
while (++$x <= 3)
{
$key = key($n);
$value = current($n);
next($n);
echo "Key : " . $key . " Value : " . $value . '<br>' ;
}
#5
0
Easier I would think:
简单的我认为:
arsort($n);
$three = array_chunk($n, 3, true)[0];
//or
$three = array_slice($n, 0, 3, true);