I tried to get the 2nd highest value from an array and I simply want to know if it is possible to do something like MAX -1 OR I absolutely need to sort by table and the 2nd highest value
private function max_key($array) {
foreach ($array as $key => $val) {
if ($val == max($array)) return $key;
}
}
So yes, you’d need to
rsort()(built-in PHP function, sorts the array with highest values first), and then take the 2nd value from the list.Note that e.g.
array( 1 , 10 , 5 , 10 )has second value == first value; if you want the second-largest unique value, run it througharray_unique()first.