[英]Find the second highest variable in array
I would like to find the second highest variable in an array. 我想找到数组中第二高的变量。
For example if I have: 例如,如果我有:
$cookies = array(
"chocolate" => "20",
"vanilla" => "14",
"strawberry" => "18",
"raspberry" => "19",
"bluebery" => "29"
);
I can use max($cookies)
to find the highest variable, which is "bluebery" => "29"
. 我可以使用
max($cookies)
来查找最高变量,即"bluebery" => "29"
。
But how do I find the second highest? 但我如何找到第二高?
"chocolate" => "20"
Sort it and get the second item is the easiest way: 对它进行排序并获得第二项是最简单的方法:
arsort($cookies);
$keys = array_keys($cookies);
echo $keys[1]; // chocolate
echo $cookies[$keys[1]]; // 20
If you want a more efficient way, you can also do it manually, by keeping track of both the highest and second-highest items at the same time: 如果您想要一种更有效的方式,您也可以通过同时跟踪最高和第二高的项目来手动完成:
function secondMax($arr) {
$max = $second = 0;
$maxKey = $secondKey = null;
foreach($arr as $key => $value) {
if($value > $max) {
$second = $max;
$secondKey = $maxKey;
$max = $value;
$maxKey = $key;
} elseif($value > $second) {
$second = $value;
$secondKey = $key;
}
}
return array($secondKey, $second);
}
Usage: 用法:
$second = secondMax($cookies);
echo "{$second[0]} => {$second[1]}"; // chocolate => 20
For fun you could use max()
twice :) 为了好玩,你可以使用
max()
两次:)
For example: 例如:
max()
max()
max()
again max()
The alternative would to sort the array based on values and get the second element of the array. 另一种方法是根据值对数组进行排序,并获取数组的第二个元素。 I'd be curious which is faster .
我很好奇哪个更快 。 Likely the sort.
可能是那种。
arsort($cookies) AND array_shift($cookies) AND list($k, $v) = each($cookies);
echo "$k => $v"; // chocolate => 20
Try : 试试:
asort($cookies);
end($cookies);
prev($cookies);
list($key,$value) = each($cookies);
or reverse it 或者反转它
arsort($cookies);
reset($cookies);
next($cookies);
list($key,$value) = each($cookies);
** Edit ** ** 编辑 **
I thought I'd share this anyway, if someone would stumble across this and need it : 无论如何,我想我会分享这个,如果有人偶然发现并需要它:
/**
* Returns the key => value pair with the specific rank.
* if $rank is 0, falase is returned. If $rank is positive,
* then the $rank th smallest pair is returned. If $rank
* is negative, then the $rank th biggest pair is returned.
* If $rank range is outside the size of the array, false
* is returned. If a callable function is provided, it will
* be used to sort the data. If $keySort is true, then the
* data will be sorted by keys instead (the callback functions
* will receive the keys to compare instead of values)
*
* @param $data array
* @param $rank int
* @param $cmd_function callable (optional)
* @param $keySort boolean (optional)
* @return array the key => value pair or false
*/
function findByRank($data, $rank, $cmd_function = null, $keySort = false) {
if (($rank == 0) || (abs($rank) > count($data))) {
return false;
}
$sort = ($keySort?'k':'a').'sort';
if ($cmd_function != null) {
$sort = 'u'.$sort;
$sort($data, $cmd_function);
} else {
$sort($data);
}
if ($rank > 0) {
reset($data);
$next = 'next';
} else {
end($data);
$next = 'prev';
$rank = abs($rank);
}
while (--$rank > 0) $next($data);
return each($data);
}
$cookies = array(
"chocolate" => "20",
"vanilla" => "14",
"strawberry" => "18",
"raspberry" => "19",
"bluebery" => "29"
);
header('Content-type:text/plain; charset=utf-8');
var_dump(findByRank($cookies, -10)); // -> false
var_dump(findByRank($cookies, -2)); // -> 'chocolate' key=>value pair
var_dump(findByRank($cookies, -1)); // -> 'blueberry' key=>value pair
var_dump(findByRank($cookies, 0)); // -> false
var_dump(findByRank($cookies, 1)); // -> 'vanilla' key=>value pair
var_dump(findByRank($cookies, 3)); // -> 'raspberry' key=>value pair
rsort($cookies);
echo $cookies[1];
Sort the array descending and take the second value. 对数组进行降序排序并获取第二个值。 Or, to be save, take the first value and go through the array until you find a smaller one.
或者,要保存,请取第一个值并遍历数组,直到找到较小的数组。
Check this URL 检查此URL
http://maheshbokkisam.blogspot.in/2013/04/find-nth-highest-value-in-array-without.html http://maheshbokkisam.blogspot.in/2013/04/find-nth-highest-value-in-array-without.html
Find Nth/ N th highest value from given array without using any sorting in PHP 从给定数组中找出第N / N个最高值,而不使用PHP中的任何排序
$ar = array(23,56,87,12,98,85,24,54,99,100,1,4,5,2,76,37,92); $n = count($ar) - 5; for($i = 0; $i < $n; $i++){ // Get the max value from array // get the Nth value from last loop echo $a = max($ar); echo "<br /><pre>"; print_r($ar); $ar = array_flip($ar); // Flip the array //print_r($ar); unset($ar[$a]); // Unset the max value from array //print_r($ar); $ar = array_flip($ar); // Flip the array echo "</pre>"; echo "<hr />"; }
function second_largest($arr)
{
sort($arr, SORT_NUMERIC);
return($arr[count($arr) - 2]);
}
//echo 3
echo second_largest(array(0, 3, 4, 1, 2));
<?php
// Finding Second highest number (In case of index of array is random)
$arr = [-5 => 33, -4 => -2, 8 => 0, 44, 44, 44, 44, 44];
$max = -INF;
$secondMax = -INF;
$size = sizeof($arr);
if ($size > 1) {
foreach ($arr as $key => $value) {
echo "KEY-> ", $key, "VALUE->", $value, "\n";
if ($value > $max) {
$max = $value;
} else {
if ($value < $max && $value > $secondMax) {
$secondMax = $value;
}
}
}
} else if ($size == 0) {
$max = "No Max element";
$secondMax = "No Second highest element";
} else {
foreach ($arr as $key => $value) {
$max = $arr[$key];
$secondMax = "No second highest element";
}
}
echo "maxvalue = ", $max, "\n";
echo "secondmax =", $secondMax;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.