Home > Backend Development > PHP Tutorial > How to get the index value of the array item with the largest key value in an array in php

How to get the index value of the array item with the largest key value in an array in php

墨辰丷
Release: 2023-03-31 18:48:02
Original
2956 people have browsed it

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(&#39;tom&#39;=>9,&#39;jack&#39;=>3,&#39;kim&#39;=>5,&#39;hack&#39;=>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

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!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template