Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.