Page 1 of 1

determining the key of an array

Posted: Thu Oct 27, 2005 3:18 pm
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?

Posted: Thu Oct 27, 2005 3:25 pm
by feyd

Posted: Thu Oct 27, 2005 3:28 pm
by s.dot
Score. That seems less backwards-ish. :) Thanks.