【问题标题】:Displaying coordinates from mysql in a php array in specific way以特定方式在php数组中显示来自mysql的坐标
【发布时间】:2020-04-27 20:24:03
【问题描述】:

我要做的是从 MySQL 表中获取 lat 和 lon 列,并将其放入具有这种格式“lat lon”的数组中。 这是我到目前为止所做的:

<?php
    $connection = mysqli_connect("localhost", "root", "", "whichever");
    $sql = "SELECT lat, lon FROM whatever";
    $result = mysqli_query($connection, $sql);
    $latlon = array();
    $index = 0;
    while($row = mysqli_fetch_assoc($result)){ 
     $latlon[$index] = $row;
     $index++;
    }
    $points = $latlon // consider that this was initially stated as $points= array("lat lon", "lat lon", ..."lat lon")
?>

但它只是以这种方式给了我数组:

Array ( [0] => Array ( [lat] => 37.9661239 [lon] => 23.7528766 ) [1] => Array ( [lat] => 37.9661321 [lon] => 23.7528387 )

当我使用print_r($points)时。

任何帮助将不胜感激。

【问题讨论】:

  • 结果应该是什么样子?
  • SELECT CONCAT(lat, ' ', lon) AS latlon FROM whatever

标签: php mysql arrays


【解决方案1】:
while($row = mysqli_fetch_assoc($result)){ 
     $latlon[$index] = $row['lat'] . ' ' . $row['lon'];
     $index++;
}

【讨论】:

    【解决方案2】:

    试试这个:

    <?php
        $connection = mysqli_connect("localhost", "root", "", "whichever");
        $sql = "SELECT lat, lon FROM whatever";
        $result = mysqli_query($connection, $sql);
        $latlon = array();
        $index = 0;
        while($row = mysqli_fetch_assoc($result)){
        $lat = $row['lat'];
        $lon = $row['lon'];
        $latlon[$index] = $lat . ' ' . $lon;
        $index++;
        }
        $points = $latlon // consider that this was initially stated as $points= array("lat lon", "lat lon", ..."lat lon")
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-22
      • 2016-02-11
      • 1970-01-01
      相关资源
      最近更新 更多