Crecket
Crecket

Reputation: 718

PHP turn data into json array

I'm trying to get the coordinates from my database and show them as markers on a map using the Jvectormap plugin. But how can I turn the data which I retrieve from my database into a working json array? I've done something similar before with the morrisJs plugin so I know how to encode them to json but I'm having some issues.

As of now my code looks like this:

$sql = "SELECT latitude, longtitude, user_name FROM page_load 
INNER JOIN users ON page_load.username = users.user_ID
WHERE bot = 0 AND latitude <> 0 AND longtitude <> 0 LIMIT 10";
$sth = $conn->prepare($sql);
$sth->execute();
$arr = array();
while ($rows = $sth->fetchAll(PDO::FETCH_ASSOC)) {
    $arr = $rows;
}    
foreach($arr as $row){
    $temp = $row['latitude'].", ".$row['longtitude'];
    $temp2 = $row['user_name'];
    $newarray = array("latLng" => $temp,
        "name" => $temp2
        );  
}
?>
markers: <?php print_r(json_encode($newarray)); ?>

This returns

{"latLng":"52.5, 6","name":"crecket"},

But I need it to look like this according to the guide for this plugin:

{latLng: [52.5, 6], name: 'crecket'},

As you can see I already turned the 2 langtitude and longtitude variables into 1 key for the array but I can't seem to get rid of the quotations.

So my question really is, what steps do I need to take to turn the result I get now into the format I need?

Upvotes: 0

Views: 58

Answers (1)

John Conde
John Conde

Reputation: 219894

Just make $temp an array:

$temp = array($row['latitude'], $row['longtitude']);

Upvotes: 4

Related Questions