note 2421 modified in function.arsort by irchtml
| From: | irchtml@php.net | Date: | Wed, 25 Feb 2004 14:10:31 +0000 |
| Subject: | note 2421 modified in function.arsort by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65714@lists.php.net to get a copy of this message | ||
If you need to sort a multi-demension array, for example, an array such as
$TeamInfo[$TeamID]["WinRecord"]
$TeamInfo[$TeamID]["LossRecord"]
$TeamInfo[$TeamID]["TieRecord"]
$TeamInfo[$TeamID]["GoalDiff"]
$TeamInfo[$TeamID]["TeamPoints"]
and you have say, 100 teams here, and want to sort by "TeamPoints":
first, create your multi-dimensional array. Now, create another, single dimension array populated
with the scores from the first array, and with indexes of corresponding team_id... ie
$foo[25] = 14
$foo[47] = 42
or whatever.
Now, asort or arsort the second array.
Since the array is now sorted by score or wins/losses or whatever you put in it, the indices are all
hoopajooped.
If you just walk through the array, grabbing the index of each entry, (look at the asort example.
that for loop does just that) then the index you get will point right back to one of the values of
the multi-dimensional array.
Not sure if that's clear, but mail me if it isn't...
-mo
--was--
If you need to sort a multi-demension array, for example, an array such as
$TeamInfo[$TeamID]["WinRecord"]
$TeamInfo[$TeamID]["LossRecord"]
$TeamInfo[$TeamID]["TieRecord"]
$TeamInfo[$TeamID]["GoalDiff"] $TeamInfo[$TeamID]["TeamPoints"]
and you have say, 100 teams here, and want to sort by "TeamPoints":
first, create your multi-dimensional array. Now, create another, single dimension array populated
with the scores from the first array, and with indexes of corresponding team_id... ie
<pre>$foo[25] = 14</pre>
<pre>$foo[47] = 42</pre>
or whatever.
Now, asort or arsort the second array.
Since the array is now sorted by score or wins/losses or whatever you put in it, the indices are all
hoopajooped.
If you just walk through the array, grabbing the index of each entry, (look at the asort example.
that for loop does just that) then the index you get will point right back to one of the values of
the multi-dimensional array.
Not sure if that's clear, but mail me if it isn't...
-mo
http://www.php.net/manual/en/function.arsort.php