Page 1 of 1

Sorting a pair of arrays

Posted: Mon Dec 11, 2006 6:36 pm
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

Posted: Mon Dec 11, 2006 6:45 pm
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;

Posted: Mon Dec 11, 2006 6:54 pm
by impulse()
The latter suggestion makes things easier for me.

Thank you, Stephen.