I have some JS that is sending a POST request to a PHP controller. The JS code is as follows:
$.ajax({
url: 'map-controller/coordcontroller.php',
data: {myData:JSON.stringify(myArray)},
type: 'post',
success: function(output) {
console.log(output);
}
});
On the server side, $_POST["myData"] is as follows:
[{"lat":36.8867497490586,"lng":-76.3046246767044},{"lat":36.88671756964517,"lng":-76.30381464958191}]
As I understand it, my data resides in a single element array ($_POST["myData"]), containing a series of objects delimited by commas. I have tried json_decode() but I have a feeling it isn't working because my data object is actually an array instead of actual JSON data.
My question: How can I access each object within a loop? Or is my implementation flawed and should I modify my Javascript to send the data differently?
var_dump (json_decode($_POST["myData"]));
Results in:
array(2) {
[0]=>
object(stdClass)#1 (2) {
["lat"]=>
float(36.886809817261)
["lng"]=>
float(-76.304672956467)
}
[1]=>
object(stdClass)#2 (2) {
["lat"]=>
float(36.886146919127)
["lng"]=>
float(-76.305075287819)
}
}
My mistake was using echo instead of var_dump to check the decoded object.