iarnazca Posted February 20, 2010 Share Posted February 20, 2010 I want to return the array with the highest value. <?php $vote['john'] = 1 $vote['james'] = 1 $vote['john'] = $vote['john'] + 1 ; $max = max($vote) ; echo " " . $vote['$max'] . " " ; ?> How do I get it to output "john" instead? Link to comment https://forums.phpfreaks.com/topic/192709-array-help/ Share on other sites More sharing options...
Zyx Posted February 20, 2010 Share Posted February 20, 2010 Have you checked, what max() function returns? It returns the maximum value, not the index of this value in the array, so the conclusion is simple: you have to find another way. Have you thought of writing the maximum value searching algorithm yourself, so that you could program it to find the index, too. I won't give you a ready code - try to write it yourself. It's very easy: just scan the array, store the current maximum value in some extra variable and if you find a greater one, update it. At the same time, remember the index in another variable and that's all. Link to comment https://forums.phpfreaks.com/topic/192709-array-help/#findComment-1015167 Share on other sites More sharing options...
iarnazca Posted February 20, 2010 Author Share Posted February 20, 2010 Thanks for the reply. I know what max() does. I was saying that it doesn't do what I want it to and asking for direction on what to use to get what I want. Link to comment https://forums.phpfreaks.com/topic/192709-array-help/#findComment-1015170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.