How to find the maximum value and key of array in php

04-12-2024

Array in PHP is a very common data type, and sometimes it is necessary to solve the maximum value and the corresponding Key value in the array. This paper will introduce how to realize this operation in PHP.

First, find the maximum value

In PHP, there are two ways to find the maximum value in an array:

1. Use the max () function

PHP's built-in max () function can easily fetch the maximum value in the array and return it. The syntax for using this function is very simple:

$max = max($arr);

Where $arr is the array to be queried, and $max is the maximum value returned. For example:

$arr = array(2, 6, 3, 1, 8, 5); $max = max($arr); echo $max; //Output 8

Step 2 use the foreach loop

Another method is to use the foreach loop to compare the size of each element in turn and get the maximum value by comparison. The specific implementation method is as follows:

$max = $arr[0]; foreach ($arr as $value) { if ($value > $max) { $max = $value; }}

Where $arr is the array to be queried, and $max is the current maximum value. The foreach loop traverses each element to determine whether it is greater than the current maximum value, and if it is greater than the current maximum value, it is reassigned to $max.

Second, find the corresponding Key

The problem of solving the maximum value in the array has been completed, and the next problem becomes how to get the key value corresponding to this maximum value.

1. use the array_keys () and max () functions.

PHP's built-in array_keys () and max () functions can be used together, which can easily extract the key value of the largest value in the array. The format used is as follows:

$keys = array_keys($arr, max($arr));

Among them, $arr is the array to be queried, max($arr) returns the maximum value of this array, and the array_keys () function uses this maximum value to return the corresponding key value array. For example:

$arr = array('a' => 2, 'b' => 6, 'c' => 3, 'd' => 1, 'e' => 8, 'f' => 5); $keys = array_keys($arr, max($arr)); print_r($keys); //output: Array ([0] => e)

Here $arr is an associative array, and the corresponding key value of the maximum value in the array is' e', which is stored in the $keys array.

Step 2 use the foreach loop

Another method is to compare the size of each element in turn through the foreach loop to get the key value corresponding to the maximum value. The specific implementation method is as follows:

//Step 1: Get the maximum value of $max = $arr[0]; foreach ($arr as $value) { if ($value > $max) { $max = $value; }}//Step 2: Get the key value $key = array () corresponding to the maximum value; foreach ($arr as $k => $v) { if ($v == $max) { $key[] = $k; }}//Output the key value print_r($key) corresponding to the maximum value;

We first get the maximum value $max in the array through the foreach loop, and then look for the key value corresponding to the maximum value through the foreach loop again. The specific implementation method is to judge whether the current element is equal to $max at first in each loop, and if it is, store the current key value $k in the $key array, and finally output the $key array.

Third, the example demonstration

In order to demonstrate more specifically how to find the maximum value and the corresponding Key in PHP, let's make a simple example: suppose the following user information (ID, name and age) is stored in the database:

$ id _ name _ age = array (array ('id' = > 1,' name' = >' Zhang San',' age' = > 20), array ('id' = > 2,' name' = >' Li Si',' age' = > 25). Age' = > 30), array ('ID' = > 4,' Name' = >' Zhao Liu',' Age' = > 22), array ('ID' = > 5,' Name' = >' Qian Qi',' age' => 18), array.

We need to find out which user is the oldest and output this user's ID, name and age. The specific implementation method is as follows:

//Step 1: Get the maximum age $ages = array (); foreach ($id_name_age as $value) { $ages[] = $value['age']; }$max_age = max($ages); //Step 2: Get the user information $max_age_user = array () corresponding to the maximum age; foreach ($id_name_age as $value) { if ($value['age'] == $max_age) { $max_age_user = $value; break; }}//Output the user information echo' ID:'. $ max _ age _ user ["ID"].'' corresponding to the maximum age; Echo' name:'. $max_age_user["name"].''; Echo' Age:'. $max_age_user["age"].'';

Here, we first iterate through the $id_name_age array through the foreach loop to get the age of each user, and then use the max () function to get the maximum value of this array $max_age. Then use the foreach loop again to find out the user information with the age of $max_age, and store it in the array of $max_age_user, and finally output the ID, name and age of this user.

Fourth, summary

Through the above introduction and examples, we can see that it is not a very difficult operation to solve the maximum value and the corresponding Key in the array in PHP. No matter from the code implementation complexity or running efficiency, PHP has provided many effective ways to complete this operation. Therefore, mastering these methods is very helpful for daily PHP programming.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us