按数组计算的值对数组进行排序 [英] sort array by value that is calculated from the array
问题描述
我有一个用户从数据库中的用户lat和lng在一个数组
,我有我的lat和lng
现在我想计算距离和从我的数据库中排序用户
$ mylat = $ _SESSION ['lat'];
$ mylng = $ _SESSION ['lng'];
$ statement = $ pdo-> prepare(SELECT * FROM users);
$ statement-> execute();
$ users = $ statement-> fetchAll();
foreach($用户为$ row){
$ dist = 0.0;
$ x1 = $ mylng;
$ x2 = $ row ['lng'];
$ y1 = $ mylat;
$ y2 = $ row ['lat'];
$ b $ 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($用户为$ row){
$ dist = 0.0;
$ x1 = $ mylng;
$ x2 = $ row ['lng'];
$ y1 = $ mylat;
$ y2 = $ row ['lat'];
$ b $ 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
但是我不会工作。
感谢您的帮助:
解决方案创建一个距离函数:
函数getDistance($ lat,$ lon)
{
global $ mylng,$ mylat;
$ x1 = deg2rad($ mylng);
$ x2 = deg2rad($ lon);
$ y1 = deg2rad($ mylat);
$ y2 = deg2rad($ lat);
$ b $ dist = acos(sin($ x1)* sin($ x2)+ cos($ x1)* cos($ x2)* cos($ y2 - $ y1))*(6378.137) ;
return $ dist;
$ / code $
$ b $ p创建一个比较函数:
函数compareDistance($ user1,$ user2)
{
return getDistance($ user1 ['lat'],$ user1 ['lng']) - getDistance($ user2 ['lat'],$ user2 ['lng']);
$ b $ p
$ b 然后你可以通过 uasort code>:
$ b $ $ $ $ $ $ $ $ c $ uasort $ users'compareDistance');
请参阅 http://php.net/manual/en/function.uasort.php 获取更多信息。
编辑:
您的程序可以改写:
function getDistance($ lat,$ lon)
{
global $ mylng,$ mylat;
$ x1 = deg2rad($ mylng);
$ x2 = deg2rad($ lon);
$ y1 = deg2rad($ mylat);
$ y2 = deg2rad($ lat);
$ b $ dist = acos(sin($ x1)* sin($ x2)+ cos($ x1)* cos($ x2)* cos($ y2 - $ y1))*(6378.137) ;
return $ dist;
$ b $函数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');
$ b foreach($ users as $ row){
$ dist = getDistance($ row ['lat'],$ row ['lng']);
$ distn = floor(round($ dist,1)* 2)/ 2;
echo $ row ['username']。 :。 $ distn。 km距离;
}
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 :)
解决方案 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";
}
这篇关于按数组计算的值对数组进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!