Jump to content

get key by position


The Little Guy

Recommended Posts

Well, that depends on what you're trying to achieve. If you want the first key, you can reset() the array, and then use the key() function.

 

If you want to find the key of a particular value, use the array_search() function - however, im not sure this is what you want, since you have 3 elements with a value of 1.

 

Why are we selecting the key 4, rather than any of the others?

 

Edit: I think the difference in the two replies highlights the ambiguity of the question!

Link to comment
Share on other sites

So based on your sort you want the 4th key you say?

 

you can run a sort that will reindex all the keys and thus $array[3] will always be it, but if the key is importat it won't work, you could try next(next(next($array); after your sort and then say get key on that there is a function for get key

Link to comment
Share on other sites

If you only want the first element from an array use array_shift:

 

        case 'mode':
            $v = array_count_values($array);
            arsort($v);
            $total = array_shift(array_keys($v));
        break;

 

not a loop.

Link to comment
Share on other sites

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.