Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


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

28,736 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....