Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Get Highest Value from Array

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 labels - which makes it pointless for what I need. Here's the array:

array("a"=>1,"b"=>2,"c"=>4,"d"=>5); 

Any ideas?

like image 527
Jamie McElwain Avatar asked Jul 13 '11 09:07

Jamie McElwain


1 Answers

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); 
like image 184
Karoly Horvath Avatar answered Oct 15 '22 20:10

Karoly Horvath