sort array by value that is calculated from the array

喜欢而已 提交于 2020-01-11 12:04:07

问题


I have the lat and lng from a user from the database in an array and I have my lat and lng

Now I want to calculate the distance and sort the users from my database with that

$mylat = $_SESSION['lat'];
$mylng = $_SESSION['lng'];

$statement = $pdo->prepare("SELECT * FROM users");
$statement->execute();
$users = $statement->fetchAll();

foreach($users as $row){
    $dist = 0.0;
    $x1 = $mylng;
    $x2 = $row['lng'];
    $y1 = $mylat;
    $y2 = $row['lat'];

    $dist = acos(sin($x1=deg2rad($x1))*sin($x2=deg2rad($x2))+cos($x1)*cos($x2)*cos(deg2rad($y2) - deg2rad($y1)))*(6378.137);
    $distn = FLOOR ( ROUND($dist,1) * 2 ) / 2 ;
}

sort($distn);

foreach ($users as $row) { 

$dist = 0.0;
    $x1 = $mylng;
    $x2 = $row['lng'];
    $y1 = $mylat;
    $y2 = $row['lat'];

    $dist = acos(sin($x1=deg2rad($x1))*sin($x2=deg2rad($x2))+cos($x1)*cos($x2)*cos(deg2rad($y2) - deg2rad($y1)))*(6378.137);
    $distn = FLOOR ( ROUND($dist,1) * 2 ) / 2 ;

echo $row['username'];
echo $distn;

}

So in the first foreach I calculate the distance of each user to me. Than I want to sort the users after the distance to me and display them with there name and there distance to me.

user1 0.5km distance
user2 1km distance

But I wont work. Thanks for your help :)


回答1:


Create a distance function:

function getDistance ($lat, $lon)
{
    global $mylng, $mylat;
    $x1 = deg2rad($mylng);
    $x2 = deg2rad($lon);
    $y1 = deg2rad($mylat);
    $y2 = deg2rad($lat);

    $dist = acos(sin($x1)*sin($x2)+cos($x1)*cos($x2)*cos($y2 - $y1))*(6378.137);
    return $dist;
}

Create a comparison function:

function compareDistance ($user1, $user2)
{
    return getDistance ($user1['lat'], $user1['lng']) - getDistance ($user2['lat'], $user2['lng']);
}

Then you can pass your array through uasort:

uasort ($users, 'compareDistance');

See http://php.net/manual/en/function.uasort.php for more information.

EDIT:

Your program could be rewritten:

function getDistance ($lat, $lon)
{
    global $mylng, $mylat;

    $x1 = deg2rad($mylng);
    $x2 = deg2rad($lon);
    $y1 = deg2rad($mylat);
    $y2 = deg2rad($lat);

    $dist = acos(sin($x1)*sin($x2)+cos($x1)*cos($x2)*cos($y2 - $y1))*(6378.137);
    return $dist;
}

function compareDistance ($user1, $user2)
{
    return getDistance ($user1['lat'], $user1['lng']) - getDistance ($user2['lat'], $user2['lng']);
}

$mylat = $_SESSION['lat'];
$mylng = $_SESSION['lng'];

$statement = $pdo->prepare("SELECT * FROM users");
$statement->execute();
$users = $statement->fetchAll();

uasort ($users, 'compareDistance');

foreach ($users as $row) { 
    $dist = getDistance ($row['lat'], $row['lng']);
    $distn = floor(round($dist,1) * 2) / 2 ;

    echo $row['username']. ": " . $distn . "km distance";
}


来源:https://stackoverflow.com/questions/42188330/sort-array-by-value-that-is-calculated-from-the-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!