Home > Backend Development > PHP Tutorial > How to Find the Index of the Maximum Value in an Array?

How to Find the Index of the Maximum Value in an Array?

Mary-Kate Olsen
Release: 2024-10-29 06:30:30
Original
548 people have browsed it

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
)
Copy after login

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>
Copy after login

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]
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template