sorting an array
| From: | Steve Buehler | Date: | Thu, 20 Mar 2003 14:32:57 +0000 |
| Subject: | sorting an array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-140433@lists.php.net to get a copy of this message | ||
I am having problems sorting a multi-dimensional array. I am hoping that somebody can help me figure out what I am doing wrong. It should sort on the "field" than on the "fac". Here is my code:
-----start code-----
$sortterm;
get_locations();
function cmp ($a, $b) {
GLOBAL $sortterm;
return strcmp($a[$sortterm], $b[$sortterm]);} function get_locations(){ GLOBAL $sortterm; $i="1"; while(($row=mysql_fetch_object($result))){ $locs[$i][loc_id]=$row->loc_id; $locs[$i][fac]=$row->fac; $locs[$i++][field]=$row->field; } reset($locs); $sortterm="field"; usort($locs, "cmp"); reset($locs); $sortterm="fac"; usort($locs, "cmp"); reset($locs); while (list ($key, $value) = each ($locs)) { echo $locs[$key][loc_id].", ".$locs[$key][fac].", ".$locs[$key][field]."<br>\n"; } } -----end code----- Here is how it comes out when run: 12, Johnson County Community College, C 11, Johnson County Community College, B 13, Johnson County Community College, D 14, Johnson County Community College, E 10, Johnson County Community College, A 8, Johnson County Girls Athletic Assoc, D 6, Johnson County Girls Athletic Assoc, B 7, Johnson County Girls Athletic Assoc, C 5, Johnson County Girls Athletic Assoc, A 9, Shawnee Civic Center, 1 1, Tomahawk Sports Dome, 1 2, Tomahawk Sports Dome, 2 3, Tomahawk Sports Dome, 3 4, Tomahawk Sports Dome, 4 As you can see, the fac is sorted correctly, but the field isn't. Thanks Steve -- This message has been scanned for viruses and dangerous content by the MailScanner at ow5, and is believed to be clean.