Jump to content

how to sort multi-dimensional array with specified dimension ?


vesper8

Recommended Posts

Hey all.. sorry if the topic title looks misleading.. perhaps I'm not completely sure how to formulate this question properly ;p

I'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 for

sort($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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.