在php中的while函数中显示最小值吗?
这是我的代码
$lat = $_POST['lat'];
$lon = $_POST['lon'];
include 'koneksi.php';
$query = mysqli_query($con, "SELECT * FROM node");
$col = mysqli_num_rows($query);
while($row = $query->fetch_object()){
$rad = 0.0174532925;
$r = 6371;
$lat1 = $lat * $rad;
$lon1 = $lon * $rad;
$lat2 = $row->latitude * $rad;
$lon2 = $row->longitude * $rad;
$x = ($lon2 - $lon1) * (cos(($lat1 + $lat2) / 2));
$y = ($lat2 - $lat1);
$h = ((sqrt($x * $x + $y * $y) * $r));
$hasil_jarak = number_format($h, 3, ".", ",");
echo $hasil_jarak."<br>";
}
如何显示hasil_jarak中的最小值?
我在echo下方使用了min()函数,但是下面的错误是错误消息。 消息:“仅给出一个参数时,它必须是数组”。
如何解决?
答案 0 :(得分:1)
在循环中跟踪最小值可能最简单:
$minh = PHP_FLOAT_MAX;
while($row = $query->fetch_object()){
$rad = 0.0174532925;
$r = 6371;
$lat1 = $lat * $rad;
$lon1 = $lon * $rad;
$lat2 = $row->latitude * $rad;
$lon2 = $row->longitude * $rad;
$x = ($lon2 - $lon1) * (cos(($lat1 + $lat2) / 2));
$y = ($lat2 - $lat1);
$h = ((sqrt($x * $x + $y * $y) * $r));
if ($h < $minh) $minh = $h;
$hasil_jarak = number_format($h, 3, ".", ",");
echo $hasil_jarak."<br>";
}
echo "minimum value is " . number_format($minh, 3, ".", ",");