Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 按从数组计算的值对数组进行排序_Php_Arrays_Sorting_Floating Point - Fatal编程技术网

Php 按从数组计算的值对数组进行排序

Php 按从数组计算的值对数组进行排序,php,arrays,sorting,floating-point,Php,Arrays,Sorting,Floating Point,我从数据库中的一个用户那里得到了一个数组中的lat和lng 我有我的lat和lng 现在我想计算距离,并用它对数据库中的用户进行排序 $mylat = $_SESSION['lat']; $mylng = $_SESSION['lng']; $statement = $pdo->prepare("SELECT * FROM users"); $statement->execute(); $users = $statement->fetchAll(); foreach($us

我从数据库中的一个用户那里得到了一个数组中的lat和lng 我有我的lat和lng

现在我想计算距离,并用它对数据库中的用户进行排序

$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;

}
因此,在第一个foreach中,我计算每个用户到我的距离。 然后,我想根据距离对用户进行排序并显示它们 有名字,有距离

user1 0.5km distance
user2 1km distance
但我不会工作。
感谢您的帮助:)

创建距离函数:

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']);
}
创建一个比较函数:

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']);
}
然后可以通过
uasort
传递数组:

uasort ($users, 'compareDistance');
有关更多信息,请参阅

编辑:

您的程序可以重写:

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";
}

sort($distn)
,$dstn看起来不像arraySadasivan Pillai,$distn是使用数组中的值为数组中的每个值计算的值。数组:user1=>lat=>lng,user2=>lat=>lang,等等。。。我想计算一个用户和我之间的距离,并将其排序为我有两个以上的用户。我想计算创建的每个用户的距离,并对它们进行排序,以便首先显示最近的用户。所以lat和lng来自我数据库中的一个数组。我必须把函数放在第一个foreach循环中吗?有两个函数,你的意思是什么?我再次告诉你我的问题。在下面。谢谢你试着帮助我