Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return index of highest value in an array

Tags:

arrays

php

From an array that looks something like the following, how can I get the index of the highest value in the array. For the array below, the desired result would be '11'.

Array (     [11] => 14     [10] => 9     [12] => 7     [13] => 7     [14] => 4     [15] => 6 ) 
like image 235
Jeff Avatar asked Sep 22 '09 17:09

Jeff


People also ask

What is the highest index of an array in Java?

A Java program can only allocate an array up to a certain size. It generally depends on the JVM that we're using and the platform. Since the index of the array is int, the approximate index value can be 2^31 – 1. Based on this approximation, we can say that the array can theoretically hold 2,147,483,647 elements.

How do you find the index value of an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.


1 Answers

My solution is:

$maxs = array_keys($array, max($array)) 

Note:
this way you can retrieve every key related to a given max value.

If you are interested only in one key among all simply use $maxs[0]

like image 198
drAlberT Avatar answered Nov 08 '22 23:11

drAlberT