Page 1 of 1

Getting position and value in an array

Posted: Sun Jan 14, 2007 4:33 pm
by GeXus
I have an array which each key is associated to an ID in my DB and the associated name.

I'm using this for posting a form which changes dynamically. How would you go about getting this data? I can do foreach, but I can only seem to get the value, without the key associated, which i need.

Thanks!

Posted: Sun Jan 14, 2007 4:39 pm
by decipher
use

Code: Select all

$keys = array_keys($array);
that will return an array with the values as the key names, and the keys as numbers so its easy to retrieve the key names.
EG:

array {
[0] => "ID",
[1] => "name"
}

Posted: Sun Jan 14, 2007 4:44 pm
by feyd
foreach() supports giving you the key.

Posted: Sun Jan 14, 2007 4:51 pm
by decipher
^^ doh! forgot bout that.

Code: Select all

foreach($array as $key => $value)
{}