4

I'm grabbing data from a mysql database and encoding a JSON object with PHP to use in JS. On the PHP end, I did this

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 the data in JS using jQuery, and I made an array to save the JSON data:

$.getJSON("getjson.php", function(data)

{
     myMarkers = data;
     console.log(myMarkers);
});

I'd planned to access the data in the myMarkers array inside loop, with a statement like this:

var tempLat = myMarkers.jmarkers[i].lat;

The problem is my JSON objects aren't called jmarkers or anything else, they have this generic 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 the PHP JSON encode function and I can't see where to set or change the object name. Any suggestions? Thank you!

3 Answers 3

5

That's to be expected. JSON is essentially the right-hand-side of an assignment operation:

var x = {'foo':'bar'};
        ^^^^^^^^^^^^^---- JSON

The x part is not included, since that's simply the name of the object. If you want your jmarkers text included, it'll have to be part of the data structure you're going to encode:

$arr = array(
   'jmarkers' => array(...your data here...);
);

But all this does is add another layer to your data structure for no useful reason.

Sign up to request clarification or add additional context in comments.

1 Comment

Actually, putting your data in a lower layer can be immensely helpful. It allows you to add top-level attributes to your responses, such as errors, exceptions, etc. It also allows you to add more types of data into your responses, ie qmarkers in addition to jmarkers.
3

$jmarkers is simply the identifier on the PHP side for the JSON object. When it gets passed, it converts the array value into a JSON-encoded string and therefore loses the identifier as a result.

In your PHP code at the moment, array_push($json, $jmarkers) is appending an array to your current $json array. You are therefore instancing a two-dimensional array, which will not be retrievable by the jmarkers identifier in your Javascript code. Simply retrieve the data using myMarkers[i] instead.

2 Comments

ok, that's good to know. so how do I change the structure of the call to my array to get at the myMarkers data? var tempLat = myMarkers.jmarkers[i].lat doesn't work, and I need to have some way of referring to the JSON objects...
It looks like $json is only being pushed once (there's only one array). Try myMarkers.id, myMarkers.lat, etc.
1

You... don't. The whole thing is an object. You only need to refer to the elements within.

alert(myMarkers.id);

1 Comment

When someone just doesn't see the forest because of the trees.. +1 from me :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.