Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find the maximum and subscript of an array by php

Shulou Source: shulou.com Published: 2022-05-31 18:16:01 09月28日 Update

This article mainly introduces php how to find the array maximum and subscript related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this php article on how to find the array maximum and subscript, let's take a look.

Php to find the array maximum and subscript method: 1, use max () to get the maximum value of the array, syntax "$max=max ($arr);"; 2, use array_search () to search for the maximum value in the array, will return the corresponding subscript value, syntax "array_search ($max,$arr)".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of finding the maximum and subscript of an array by php

Php finding the maximum and subscript of an array can be divided into two parts:

Get the maximum value of the array

Find the subscript (key name) of the value in the array according to the maximum value

Next, let's introduce the implementation method in detail.

1. Get the maximum value of the array

There are several ways for PHP to get the maximum value of an array, such as sorting (ascending or descending), so that the element at the beginning or end is the maximum value.

But because the subscript is also obtained, sorting is not necessarily optional (it may break its original order).

So let's just use the built-in function-- max ().

The max () function can return the maximum value in an array

2. Find the subscript (key name) of the value in the array according to the maximum value

PHP provides a built-in function-- array_search ()

This function searches the array for a given value and returns the corresponding key name (subscript) if successful.

$index=array_search ($max,$arr); the subscript of the maximum value of echo is:. $index

This is the end of the article on "how to find the maximum and subscript of an array by php". Thank you for reading! I believe you all have a certain understanding of "how to find the maximum and subscript of an array in php". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Array maximum maximum subscript function method knowledge content article grammar sort search success two value element ascending multiple that is the beginning Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info OPPO Reno Redmi Docker NVidia