PHP Get Highest Value from Array

前端 未结 16 1333
耶瑟儿~
耶瑟儿~ 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:42

    If you just want the largest value in the array use the max function. This will return the largest value, although not the corresponding key. It does not change the original array.

    If you care about the the key you could then do

    $key = array_search(max($array), $array)
    

    (Edited to include @binaryLV's suggestion)

提交回复
热议问题