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
https://forums.phpfreaks.com/topic/76653-get-key-by-position/#findComment-388109
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
https://forums.phpfreaks.com/topic/76653-get-key-by-position/#findComment-388110
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
https://forums.phpfreaks.com/topic/76653-get-key-by-position/#findComment-388210
Share on other sites

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.