Jalon Juanis Jalon Juanis - 10 days ago 6
PHP Question

SyntaxError:missing ; before statement [PHP array to Javascript Array]

This is how I get php array from api using php file_get_contents

<?php
$array=json_decode(file_get_contents("https://www.example.com.my/sandbox/api/mobileapp.php?token=123123&action=listing_for_map"), true);
$arr= json_encode($array['get']);
?>


Here how I put the php array to javascript array and how I get the data

var geoPoint = '<?php echo $arr ;?>';

for (var i = 0; i < 1000; ++i) {
var latLng = new google.maps.LatLng(geoPoint.get[i].google_coordinate)
var marker = new google.maps.Marker({
position: latLng,
draggable: true,
icon: markerImage
});
markers.push(marker);
}


Each time I run the code, I got this error. SyntaxError:missing ; before statement

Error

Can anyone tell me where actually I did wrong? Thank in advance

Answer

Wrapping your JSON in single quotes defines it as a string. You want it to be defined as an object:

var geoPoint = <?php echo $arr ;?>;

As others' comments pointed out, you are also missing a semicolon on this line:

var latLng = new google.maps.LatLng(geoPoint.get[i].google_coordinate);