我正在使用google maps api创建地图。
基本上,调用任何标记的数组结构如下:
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]
];
现在,我将使用json调用创建一个数组,所以... 第一步我用 php 调用标记并将它们放在json_encode
中$sth = mysql_query("SELECT * FROM markers");
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$rows[] = $r;
}
print json_encode($rows);
结果
[{"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 我用jquery getJSON数据调用,就像这样......
$.getJSON( "markers.php", function( data ) {
var station = [];
$.each( data, function( key, val ) {
station.push( data );
});
});
但这完全失去了工作,所以......我能以哪种方式做到这一点? 那么......有没有办法完成这个结构?
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]
];
答案 0 :(得分:1)
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
}
});
答案 1 :(得分:0)
如果您只关心数组值,请不要获取关联数组,请尝试以下操作:
$rows = array();
while($r = mysql_fetch_array($sth)) {
$rows[] = array_values($r);
}
print json_encode($rows);