determining the key of an array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

determining the key of an array

Post by s.dot »

I have a $_POST value, that will be an element in an array.

Like this example:

Code: Select all

$name = $_POST['name']; // we'll say the name is Bob

$namesa = mysql_query("SELECT names FROM table WHERE foo = '$bar'");
$names = $namesa['names'];

$array = explode(";",$names);  // now I have Array ([0] => Jimmy, [1] => Bob, [2] => Steve)
How can I select the key from the array, where the value is Bob (the $name variable)?

Edit:

After thinking about it, I could do

Code: Select all

$new = array_flip($array);
$key = $array[$name];
but that seems a bit backwards. is there any other way to do it?
Last edited by s.dot on Thu Oct 27, 2005 3:27 pm, edited 2 times in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Score. That seems less backwards-ish. :) Thanks.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply