[SOLVED] obtaining the index order of a key in an associate

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
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

[SOLVED] obtaining the index order of a key in an associate

Post by davidklonski »

Hi

assuming I have the following array:

Code: Select all

$arr = Array("bob" => "aaa", "alice" => "bbb");
Now assuming that I have a variable holding a name of a key in the array, for example:

Code: Select all

$var = "alice";
How can I find the numeric order of that key in the array.
I.e. "bob" is the first key, so its order is 0, "alice" is 1,...

thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

array_search($var, keys($arr));
or similar, I think.
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

Post by davidklonski »

you are right.
Stupid me.

thanks
Post Reply