vesper8 Posted November 16, 2006 Share Posted November 16, 2006 Hey all.. sorry if the topic title looks misleading.. perhaps I'm not completely sure how to formulate this question properly ;pI'm new to arrays you see so I may of done this a bit weirdly.. but it does work.. without the sorting part. Here's my code[code]<table style="font-family:Verdana,Arial;font-size:10px;color:black;" width="570px"> <tr align="center"> <td><b style="color:white;">Ligue</td> <td><b style="color:white;">Nombre d'équipes</td> <td><b style="color:white;">Moyenne des cotes</td> </tr><?$regionsRS = $db->execute("SELECT DISTINCT RegionID FROM equipes WHERE ClassID=".$ClassID);$y = $regionsRS->getNumOfRows();$filterStr="0";for ($j=0;$j<$y;$j++){ $filterStr=$filterStr.",".$regionsRS->fields["RegionID"]; $regionsRS->nextRow();}$requete="SELECT * FROM tblRegions WHERE ID IN (".$filterStr.") ORDER BY Name";$regionsRS = $db->execute($requete);$y = $regionsRS->getNumOfRows();for ($j=0;$j<$y;$j++){ $requete="SELECT Count(ID) as nbTeams, sum(cote) as totalCote FROM equipes WHERE RegionID = ".$regionsRS->fields["ID"]." AND ClassID=".$ClassID; $countRS = $db->execute($requete); $moyLig_array[$j][0]=$regionsRS->fields["Name"]; $moyLig_array[$j][1]=$countRS->fields["nbTeams"]; $moyLig_array[$j][2]=round($countRS->fields["totalCote"]/$countRS->fields["nbTeams"],1); $regionsRS->nextRow();} // end forsort($moyLig_array);for ($j=0;$j<$y;$j++){ if ($bgColor!="#efefef"){ $bgColor="#efefef"; }else{ $bgColor="#ffffff"; }?> <tr align="center" style="cursor:hand;" onClick="location.href='categories.php?ClassID=<? echo $ClassID;?>&RegionID=<? echo $regionsRS->fields["ID"];?>';"> <td bgcolor="<? echo $bgColor;?>"><? echo $moyLig_array[$j][0]; ?></td> <td bgcolor="<? echo $bgColor;?>"><? echo $moyLig_array[$j][1]; ?></td> <td bgcolor="<? echo $bgColor;?>"><? echo $moyLig_array[$j][2]; ?></td> </tr> <? $regionsRS->nextRow();} // end for?></table>[/code]Basically.. I made what I'm going to call an array.. although it's really more like a table.. Each row is divided into 3 columns. The 3rd column contains a float value. And I'd like to sort my table/array on that float value before I echo it out in my table.Could anyone be so kind as to show me how to do this please?Thanks much! Link to comment https://forums.phpfreaks.com/topic/27400-how-to-sort-multi-dimensional-array-with-specified-dimension/ Share on other sites More sharing options...
btherl Posted November 16, 2006 Share Posted November 16, 2006 usort() is good for this[code=php:0]usort(&$moyLig_array, "third_field_cmp");function third_field_cmp($a, $b) { if ($a[2] < $b[2]) return -1; if ($a[2] > $b[2]) return 1; return 0;}[/code]Those comparison functions are confusing at first, but are very powerful once you get used to them. The comparison function defines the ordering by saying how to compare two items from your array. $a and $b are two array items. Link to comment https://forums.phpfreaks.com/topic/27400-how-to-sort-multi-dimensional-array-with-specified-dimension/#findComment-125316 Share on other sites More sharing options...
vesper8 Posted November 16, 2006 Author Share Posted November 16, 2006 thanks a lot btherl !that worked like a charm Link to comment https://forums.phpfreaks.com/topic/27400-how-to-sort-multi-dimensional-array-with-specified-dimension/#findComment-125327 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.