基于经度和纬度的usort php数组(usort php array based on longitude and latitude)

编程入门 行业动态 更新时间:2024-10-28 11:29:03
基于经度和纬度的usort php数组(usort php array based on longitude and latitude)

我试图对一个有一个名为“经度”和“纬度”的变量的表进行排序。 我使用下面的代码对应给定的坐标(45,45)。 它正在对某些内容进行排序,但奇怪的是在我的列中水平排序,而不是行.....

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

当我var_dump时,这就是结果(这是表格第一行的内容!!)让我疯了!!:

{ [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" }

正确答案:

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

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

最满意答案

你的距离函数没有看到$mylat , $mylong变量。 (你应该在函数体的第一行之前添加一个global $mylat, $mylong;

Your distance function doesn't see the $mylat, $mylong variables. (You should add a global $mylat, $mylong; before the first line of the function body.

更多推荐

本文发布于:2023-08-06 11:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1450051.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:经度   纬度   数组   usort   longitude

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!