Home > Backend Development > PHP Tutorial > php获取数组中键值最大数组项的索引值[原创]_php技巧

php获取数组中键值最大数组项的索引值[原创]_php技巧

PHP中文网
Release: 2016-05-16 20:20:36
Original
2921 people have browsed it

本文实例讲述了php获取数组中键值最大数组项的索引值的方法。分享给大家供大家参考。具体分析如下:

一、问题:

从给定数组中获取值最大的数组项的键值。用途如:获取班级得分最高的学生的姓名。

二、解决方法:

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

以上就是php获取数组中键值最大数组项的索引值[原创]_php技巧的内容,更多相关内容请关注PHP中文网(www.php.cn)!


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