I am creating the associative array in php in a loop like this:
$coordinates[] = array("coordinates"=>[$lat, $lng], "site"=>"ext");
$coordinates[] = array("coordinates"=>[$lat, $lng], "site"=>"curr");
Which gives:
[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"
}
}
Now I Need to push each pair coords eg:
["coordinates"]=>
array(2) {
[0]=>
string(10) "40.8998985"
[1]=>
string(9) "9.5177142"
}
into a JS array and do the same for another array with [site] values eg:
["site"]=>
string(3) "ext"
In JS I do:
var coordsJson = '<?php echo json_encode($coordinates); ?>';
var coords = JSON.parse(coordsJson);
console.log(coordsJson);
And that gives me:
[{"coordinates":["40.836132854296686","8.404270310882566"],"site":"curr"},{"coordinates":["40.8998985","9.5177142"],"site":"curr"},{"coordinates":["40.71976910000001","8.563560499999994"],"site":"ext"}]
I'd need the coords to be a valid array with the pair coords as objects and push [site] to an array like site = [] but I am not sure how to get values form the associative array.
I tried to push
myCoords.push(coords['coordinates']);
But that's wrong. I believe I should be looping the json response but how to push to JS then?
UPDATE
I basically need the js array structured like this:
0: (2) ["40.836132854296686", "8.404270310882566"]
1: (2) ["40.8998985", "9.5177142"]
2: (2) ["40.71976910000001", "8.563560499999994"]
And have the same but for an array site[], literally looking for 2 arrays coords[] and site[]