Question: 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;
    }
    }

```

Answer: 
Seems that there is no such standart function(It's too specific).  

You may sort and get second value, but it's `O(n log n)` complexity.

There is `O(n) solution`

```
function second_key($array){
    $max=null;
    $second = null;
    foreach($array as $k => $v){
        if(!isset($max) || $v > $array[$max]){
            $second = $max;
            $max = $k;
        }
        elseif(!isset($second) || $v > $array[$second]){
             $second = $k;
        }
    }
    return $second;
}

```
```
sort( $array, SORT_NUMERIC );
echo $array[ count( $array ) - 2 ];

```