Sorting a pair of arrays

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
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Sorting a pair of arrays

Post by impulse() »

I have put the values of 2 columns in a MySQL DB into 2 arrays using the following:

Code: Select all

$query = mysql_query("SELECT *
                        FROM teams");

  while ($res = mysql_fetch_array($query)) {

    $team[$i] = $res['team'];
    $score[$i] = $res['score'];
    $i++;
  }
I want to sort them descendingly so the team with the highest score is array key 0 and the rest of the values descend but once I do this the scores no longer match the teams they correspond to. How am I able to get around this problem?

Regards, Stephen
Sloth
Forum Newbie
Posts: 18
Joined: Thu Dec 07, 2006 7:29 pm

Post by Sloth »

Well, you *could* use arsort() / asort() which would mantain the index association.


Alternatively you could modify your query to

Code: Select all

SELECT * FROM teams ORDER BY score desc;
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Post by impulse() »

The latter suggestion makes things easier for me.

Thank you, Stephen.
Post Reply