1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how do I sort a php array

Discussion in 'PHP' started by dave487, Aug 23, 2004.

  1. #1
    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++ ;
      }
    PHP:
    How do I order this list by $distance_in_miles so that the shops are listed nearest first?

    many thanks.
     
    dave487, Aug 23, 2004 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    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() function.
     
    digitalpoint, Aug 23, 2004 IP
  3. vinyl

    vinyl Well-Known Member

    Messages:
    302
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    165
    #3
    you should use sort function - heres small demo:

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

    For reverse, use rsort().

    Mike
     
    vinyl, Aug 23, 2004 IP
  4. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    dave487, Aug 24, 2004 IP
  5. l0cke

    l0cke Active Member

    Messages:
    178
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    73
    #5
    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().
     
    l0cke, Aug 24, 2004 IP
  6. aenigma

    aenigma Peon

    Messages:
    42
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Can you post the SQL that extract the data from the database?
     
    aenigma, Sep 30, 2004 IP