Jump to content

How to grab the index of the first "row" in an array..??


physaux

Recommended Posts

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  :confused:

 

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 :rtfm::shrug:

 

Thanks in advance!

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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.