physaux Posted January 19, 2010 Share Posted January 19, 2010 Hey guys I am having some trouble getting what I want from an array. I had an array like this: array[12] = 2 array[21] = 33 array[19] = 3 and I sorted it using arsort(), so it now looks like so: array[21] = 33 array[19] = 3 array[12] = 2 My question is, how can I "grab" the index of the first array? (The one with the biggest value) In this case what I want is the number 21 Could anyone please clear this up for me! I tried using array[1], but that is now working. I am reading php's information about arrays, but it is so long and I still haven't found the answer to this problem Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/189038-how-to-grab-the-index-of-the-first-row-in-an-array/ Share on other sites More sharing options...
taquitosensei Posted January 19, 2010 Share Posted January 19, 2010 something like this. There's probably an easier way. function getMaxIndex($array) { $oldindex=0; foreach($array as $index=>$value) { if($index>$oldindex) { $savedindex=$index; } $oldindex=$index; } return $savedindex; } then $array=array(21=>33,19=>3,12=>2); $maxindex=getMaxIndex($array); Link to comment https://forums.phpfreaks.com/topic/189038-how-to-grab-the-index-of-the-first-row-in-an-array/#findComment-998104 Share on other sites More sharing options...
Buddski Posted January 19, 2010 Share Posted January 19, 2010 $keys = array_keys($arr); echo $keys[0]; Is alot simpler.. Link to comment https://forums.phpfreaks.com/topic/189038-how-to-grab-the-index-of-the-first-row-in-an-array/#findComment-998105 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.