Jump to content

[SOLVED] get the key of an array


.josh

Recommended Posts

okay i'm sure this is a noob question but..damn. Anyways:

just like what it says in the subject; I need to get the key of an array. For example:


$row = array('a' => 1, 'b' => 2, 'c' => 3);

well, according to the manual, doing key($row) gets the key of the associative array.  So I tried this:

$key = key($row[1]);

And thought it would assign 'b' to $key, but apparently when I read further, it really assigns the key that the array's internal pointer currently points to.  Close, but no cigar, as they say.  So I'm looking at the functions associated with arrays in the manual, and unless I'm blind (I hope I'm blind), there doesn't seem to be a function that will simply give me the key of an associative array if I specify the position like in my example above. 

So..am I blind? Anybody know how I would go about getting this? I know I can combine a foreach and a condition or 2 to get what I want, such and all, but I really don't want to go to that length for my script.  It just seems like there's got to be a way to just simply say "okay, here's my array and position, now what's the key for it."
Link to comment
https://forums.phpfreaks.com/topic/31799-solved-get-the-key-of-an-array/
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.