I am trying to sort a table that has a variable called "longitude" and "latitude". I use the code below against a given coordinate (45,45). It is sorting something, but it's strangely sorting horizontally across my columns, not the row.....
$mylat = 45;
$mylong= 45;
$res = mysql_query("SELECT * FROM account");
$users = mysql_fetch_row($res);
function distance($lon1, $lat1) { 
    $theta = $lon1 - $mylong; 
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($mylat)) +  cos(deg2rad($lat1)) * cos(deg2rad($mylat)) * cos(deg2rad($theta)); 
    $dist = acos($dist); 
    $dist = rad2deg($dist);
    return $dist;
}
function cmp($a, $b) {
    $distA = distance($a['longitude'],$a['latitude']);
    $distB = distance($b['longitude'],$b['latitude']);
    if($distA == $distB) {
    return 0;
}
    return ($distA > $distB) ? -1 : 1;
}
usort($users, "cmp");
When I var_dump, this is the results (which are the content of the first row of the table!!) Driving me mad!!:
 { [0]=> string(2) "30" [1]=> string(3) "999" [2]=> string(14) "20131018111824" [3]=> string(2) "30" [4]=> string(1) "m" [5]=> string(8) "xxxxxxxx" [6]=> string(8) "xxxxxxxx" [7]=> string(2) "45" [8]=> string(12) "xxx@gmail.com" }
CORRECT ANSWER:
$res = mysql_query("SELECT * FROM account");
   while( $row = mysql_fetch_assoc( $res)){
      $users[] = $row; // Inside while loop
   }
function distance($lon1, $lat1) { 
    GLOBAL $mylat;
    GLOBAL $mylong;
    $theta = $lon1 - $mylong; 
    $dist = sin(deg2rad($lat1)) * sin(deg2rad($mylat)) +  cos(deg2rad($lat1)) * cos(deg2rad($mylat)) * cos(deg2rad($theta)); 
    $dist = acos($dist); 
    $dist = rad2deg($dist);
    $miles = $dist * 60 * 1.1515; 
    return $miles;
}