Jump to content

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

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.