Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Giles Buchanan
Giles Buchanan
3,824 Points

finding the position of data in an array? is that the count() function?

I have an array multiple columns... its list the users, score and the game name. users can have multiple games at different scores

user_id     score      Game

1                  1000     tanks
3                 500      tanks
7                 200       submarine
1                  2000    submarine
9                 750       tanks
8                 745       tanks
1                  2000    space

I need to find for a particular user their rank for a particular game.

for example what rank is user 9 in tanks? I know I need ORDER BY score DESC, WHERE 'game' = 'tanks' but the actual counting bit has be baffled.

I'm tempted to use a for while loop but this seems clumsy?

is there a PDO/MySQL solution?

1 Answer

geoffrey
geoffrey
28,724 Points

If you want to search a particular data in an array there is this function array_search

Giles Buchanan
Giles Buchanan
3,824 Points

this seems to return the specific key associated with the array? i just need the position in an ordered array when WHERE conditions are set....