我创建的关联数组php中的一个loop是这样的:
$coordinates[] = array("coordinates"=>[$lat, $lng], "site"=>"ext");
$coordinates[] = array("coordinates"=>[$lat, $lng], "site"=>"curr");
这使:
[0]=>
array(2) {
["coordinates"]=>
array(2) {
[0]=>
string(18) "40.836132854296686"
[1]=>
string(17) "8.404270310882566"
}
["site"]=>
string(4) "curr"
}
[1]=>
array(2) {
["coordinates"]=>
array(2) {
[0]=>
string(10) "40.8998985"
[1]=>
string(9) "9.5177142"
}
["site"]=>
string(4) "curr"
}
[2]=>
array(2) {
["coordinates"]=>
array(2) {
[0]=>
string(17) "40.71976910000001"
[1]=>
string(17) "8.563560499999994"
}
["site"]=>
string(3) "ext"
}
}
现在我需要推动每一对坐标,例如:
["coordinates"]=>
array(2) {
[0]=>
string(10) "40.8998985"
[1]=>
string(9) "9.5177142"
}
到一个JS数组中,并对另一个具有[site]值的数组执行相同的操作,例如:
["site"]=>
string(3) "ext"
在JS我做:
var coordsJson = '<?php echo json_encode($coordinates); ?>';
var coords = JSON.parse(coordsJson);
console.log(coordsJson);
这给了我:
[{"coordinates":["40.836132854296686","8.404270310882566"],"site":"curr"},{"coordinates":["40.8998985","9.5177142"],"site":"curr"},{"coordinates":["40.71976910000001","8.563560499999994"],"site":"ext"}]
我需要coords是一个有效的数组,将成对坐标作为对象并推[site]送到一个数组,例如,site = []但我不确定如何从关联数组中获取值。
我试着推
myCoords.push(coords['coordinates']);
但这是错误的。我相信我应该循环的json响应,但如何push来JS呢?
更新
我基本上需要这样结构的js数组:
0: (2) ["40.836132854296686", "8.404270310882566"]
1: (2) ["40.8998985", "9.5177142"]
2: (2) ["40.71976910000001", "8.563560499999994"]
并具有相同但对于一个数组site[],字面上寻找2 个数组 coords[]和site[]
墨色风雨
杨魅力