Home > Article > Backend Development > How to Find the Index of the Maximum Value in an Array?
Identifying the Index of Maximum Value in an Array
Consider an array with assorted values like:
Array ( [11] => 14 [10] => 9 [12] => 7 [13] => 7 [14] => 4 [15] => 6 )
The goal is to determine the index of the highest value in this array, which in this case, is 14 at index 11.
Solution:
To retrieve the index of the maximum value in an array, you can leverage the following approach:
<code class="php">$maxs = array_keys($array, max($array));</code>
Using this solution, you can obtain an array of all indexes associated with the maximum value. However, if you're only interested in a single index, you can directly access it using $maxs[0].
Output:
For the provided array, the output of the solution will be:
$maxs = [11]
Hence, the index of the maximum value is 11, and the value associated with that index is 14.
The above is the detailed content of How to Find the Index of the Maximum Value in an Array?. For more information, please follow other related articles on the PHP Chinese website!