javascript - Create Array with JSON

292

i'm creating a map with google maps api.

Basically, the array structure to call any markers is like this:

var station = [
['SEDRANO DI SAN QUIRINO (PN)',46.048887,12.657924,'Via Maniago, 28','Tel. 0434 918900','sedrano-di-san-quirino.jpg',true,true,false,false,false,false,false],
['CORTINA D\'AMPEZZO (BL)',46.541307,12.132348,'Via dello Stadio, 7','Tel.  0436 869133','cortina-dampezzo.jpg',true,true,false,false,false,true,false],
['POINCICCO DI ZOPPOLA (PN)',45.948725,12.745332,'Via Cusano, 14','Tel. 0434 574831','poinciccio-di-zoppola.jpg',true,true,false,false,false,true,false]
];

Now, i would create an array with json calls, so... first step i call markers with php and put these in json_encode

$sth = mysql_query("SELECT * FROM markers");
$rows = array();
 while($r = mysql_fetch_assoc($sth)) {
  $rows[] = $r;
 }
print json_encode($rows);

result

[{"city":"SEDRANO DI SAN QUIRINO (PN)","lat":"46.0489","lng":"12.6579","address":"Via Maniago, 28","phone":"Tel. 0434 918900","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/sedrano-di-san-quirino.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"false","cafe":"false","verde":"true","diesel":"true","id":"1"},{"city":"CORTINA D'AMPEZZO (BL)","lat":"46.5413","lng":"12.1323","address":"Via dello Stadio, 7","phone":"Tel. 0436 869133","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/cortina-dampezzo.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"true","cafe":"false","verde":"true","diesel":"true","id":"2"},{"city":"POINCICCO DI ZOPPOLA (PN)","lat":"45.9487","lng":"12.7453","address":"Via Cusano, 14","phone":"Tel. 0434 574831","thumb":"http:\/\/www.costantin.com\/images\/thumb\/maps\/poinciccio-di-zoppola.jpg","top-verde":"false","top-diesel":"false","gpl":"false","lavaggio":"true","cafe":"false","verde":"true","diesel":"true","id":"3"}]

secondo step i call with jquery getJSON datas, like this...

$.getJSON( "markers.php", function( data ) {
    var station = [];
  $.each( data, function( key, val ) {
    station.push( data );
  });
});

But this is totally out of work, so... in which way i could do it? So... is there a way to accomplish this structure?

  var station = [
    ['SEDRANO DI SAN QUIRINO (PN)',46.048887,12.657924,'Via Maniago, 28','Tel. 0434 918900','sedrano-di-san-quirino.jpg',true,true,false,false,false,false,false],
    ['CORTINA D\'AMPEZZO (BL)',46.541307,12.132348,'Via dello Stadio, 7','Tel.  0436 869133','cortina-dampezzo.jpg',true,true,false,false,false,true,false],
    ['POINCICCO DI ZOPPOLA (PN)',45.948725,12.745332,'Via Cusano, 14','Tel. 0434 574831','poinciccio-di-zoppola.jpg',true,true,false,false,false,true,false]
    ];
408

Answer

Solution:

var stations = []; //You need an external variable to hold all the stations

$.getJSON( "markers.php", function( data ) {
    //var station = []; //Don't need this here

    //"data" is an array, not a key/value object
    /*$.each( data, function( key, val ) {
        //You are adding the JSON "data" array here, not the values
        station.push( data );
    });*/

    //Remember "data" is a JSON array, not object
    for (var i = 0; i < data.length; i++) { 
        var station = []; //Temporary variable to hold station data

        //Iterate over the current station object
        $.each(data[i], function(key, value) { 
          station.push(value); //You're not interested in the key
        });
        stations.push(station); //Add station to array of all stations
    }
});
197

Answer

Solution:

Don't fetch associative arrays if you only care about the array value, try this:

$rows = array();
 while($r = mysql_fetch_array($sth)) {
  $rows[] = array_values($r);
 }
print json_encode($rows);

People are also looking for solutions to the problem: php - simplexml 'Trying to get property of non-object in' error

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.