PHP Get Highest Value from Array

前端 未结 16 1332
耶瑟儿~
耶瑟儿~ 2020-11-28 07:05

I\'m trying to get hold of the largest value in an array, while still preserving the item labels. I know I can do this by running sort(), but if I do so I simply lose the la

16条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-11-28 07:18

    Don't sort the array to get the largest value.

    Get the max value:

    $value = max($array);
    

    Get the corresponding key:

    $key = array_search($value, $array);
    

提交回复
热议问题