Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
1  
Also, something you may run into, the order is typically longitude then latitude. Longitude is on the x axis. –  Jonathan Kuhn May 12 at 17:36
    
@JonathanKuhn Thanks ill swap them around :) –  Crecket May 12 at 17:37

1 Answer 1

up vote 4 down vote accepted

Just make $temp an array:

$temp = array($row['latitude'], $row['longtitude']);
share|improve this answer
    
Damn lol didn't expect it to be that simple. Thanks –  Crecket May 12 at 17:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.