This article mainly introduces the method of PHP to obtain the index value of the array item with the largest key value in the array. It mainly sorts the array through asort, and then uses a foreach loop to assign the array index value to a new array, thereby obtaining its The corresponding index value is of great practical value. Friends who need it can refer to the following
The example in this article describes how PHP obtains the index value of the array item with the largest key value in the array. The specific analysis is as follows:
1. Question:
Get the key value of the array item with the largest value from the given array. Usage: Get the name of the student with the highest score in the class.
2. Solution:
<?php /* * Created on 2018-6-12 * Created by www.php.cn */ $arr=array('tom'=>9,'jack'=>3,'kim'=>5,'hack'=>4); asort($arr); //print_r($arr); //输出:Array ( [jack] => 3 [hack] => 4 [kim] => 5 [tom] => 9 ) $rel=array(); foreach($arr as $k=>$v){ $rel[]=$k; } //echo $rel[0];//输出最小值:jack echo end($rel);//输出最大值:tom ?>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
The concept of PHP timestamp and time zone
Usage of PHP split string explode function
Definition and use of is_dir() function in PHP
The above is the detailed content of How to get the index value of the array item with the largest key value in an array in php. For more information, please follow other related articles on the PHP Chinese website!