I collect data from a mysql database and encode a JSON object with PHP for use in JS. At the end of PHP I did it
while ($ row = mysql_fetch_array ($ result))
{ $jmarkers = array( 'id'=> $row['id'], 'lat' => $row['lat'], 'lng' => $row['lng'], etc... ); array_push($json, $jmarkers); } $jsonstring = json_encode($json); echo $jsonstring;
I can access data in JS using jQuery and I created an array to store JSON data:
$. getJSON ("getjson.php", function (data)
{ myMarkers = data; console.log(myMarkers); });
I planned to access the data in the myMarkers array inside the loop, with a suggestion like this:
var tempLat = myMarkers.jmarkers [i] .lat;
The problem is that my JSON objects are not called jmarkers or anything else, they have this common name "Object" when I print them to the console:
Object {id = "2", lat = "40.6512", lng = "- 73.9691", more ...},
So I'm not sure how to point to them in my JS array. I looked at the PHP JSON encoding function, and I do not see where to set or change the name of the object. Any suggestions? Thanks!
source share