Mortgage - Free Advertising - Free Myspace Layouts - Cheap Flights - boy baby names

PDA

View Full Version : how do I sort a php array


dave487
Aug 23rd 2004, 9:15 am
I have some code as follows for a find nearest feature using shops from a database:

while ($row= mysql_fetch_array($result)) {
$title = $row["shopname"];
$title1 = $row["phone"];
$title8 = $row["Easting"];
$title9 = $row["Northing"];
$dist1 = abs($customer_easting - $title8);
$dist2 = abs($customer_northing - $title9);
$dist1 = $dist1^2;
$dist2 = $dist2^2;
$distance_in_metres = $dist1 + $dist2;
$distance_in_metres = $distance_in_metres^0.5;
$distance_in_miles = $distance_in_metres/$constant;
$distance_in_miles = number_format($distance_in_miles, 2);
echo "$title1 , $distance_in_miles";
$count++ ;
}

How do I order this list by $distance_in_miles so that the shops are listed nearest first?

many thanks.

digitalpoint
Aug 23rd 2004, 9:23 am
It doesn't look to me like it's actually an array, but if you put it into an array, you can sort it with the normal sort() (http://www.php.net/manual/en/function.sort.php) function.

vinyl
Aug 23rd 2004, 9:24 am
you should use sort function (http://www.php.net/manual/en/function.sort.php) - heres small demo:

<?php
$members = array("GuyFromChicago","rickbender1940","DigitalPoint");
sort($members);
foreach($members as $val)
{
print "$val ";
}
?>

OUTPUT:
DigitalPoint GuyFromChicago rickbender1940

For reverse, use rsort().

Mike

dave487
Aug 24th 2004, 7:30 am
So how would I put the values of name, phone, easting, northing and distance into an array?

I had thought that they were already in an array by using mysql_fetch_array....... :confused:

l0cke
Aug 24th 2004, 7:48 am
You could add a calculation in your mysql select query to calculate the distance_in_miles. Then you could either sort it by the calculated column in your select query (ORDER BY calculation) or use array_sort().

aenigma
Sep 30th 2004, 2:56 am
Can you post the SQL that extract the data from the database?