Mostafa Elkady
Mostafa Elkady

Reputation: 5791

mysql any array sorting problem

I have some users listed below in an order:

user1 user2 user3 user4 user5

This is just a simple list, sorted according to age.

Now a new user (user6) comes in, and according to the sorting, he fits after user 2, so the order becomes like:

user1 user2 user6 user3 user4 user5

Now I want to know at which position it is. For example it is on place3. How can i find its position when a new user comes in the list?? Please tell any general way? there is no any database etc, just a logic question.

Upvotes: 1

Views: 84

Answers (2)

Oleksandr Khomenko
Oleksandr Khomenko

Reputation: 11

May be something like this:

SELECT count(b.id)+1 row_pos
FROM users a, users b
WHERE
    a.name = 'user6'
    AND a.age > b.age

Thus it shows position of your row

Upvotes: 1

Borealid
Borealid

Reputation: 98469

What does this have to do with MySQL?

Use array_search($myuser, $users);

See https://www.php.net/manual/en/function.array-search.php .

Upvotes: 1

Related Questions