Getting position and value in 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
GeXus
Forum Regular
Posts: 631
Joined: Sat Mar 11, 2006 8:59 am

Getting position and value in an array

Post 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!
decipher
Forum Commoner
Posts: 38
Joined: Mon Mar 13, 2006 6:27 am
Location: south africa

Post 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"
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

foreach() supports giving you the key.
decipher
Forum Commoner
Posts: 38
Joined: Mon Mar 13, 2006 6:27 am
Location: south africa

Post by decipher »

^^ doh! forgot bout that.

Code: Select all

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