Skip to content
Advertisement

Displaying coordinates from mysql in a php array in specific way

What I’m trying to do is to get lat and lon columns from a MySQL table and put it in an array with this format “lat lon”. This is what I have done so far:

<?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")
?>

but it just gives me the array this way:

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

when I use print_r($points).

Any help would be appreciated.

Advertisement

Answer

while($row = mysqli_fetch_assoc($result)){ 
     $latlon[$index] = $row['lat'] . ' ' . $row['lon'];
     $index++;
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement