I'm trying to understand how my PHP script can pass an array to my JavaScript code. Using the following PHP, I pass an array:
$c = array(3,2,7);
echo json_encode($c);
My JavaScript is as follows:
$.post("getLatLong.php", { latitude: 500000},
function(data) {
arrayData = data
document.write(arrayData)
document.write(arrayData[0]);
document.write(arrayData[0]);
document.write(arrayData[0]);
});
</script>
What is printed out on the screen is
[3,2,7][3,
I'm trying to understand how json_encode works - I thought I would be able to pass the array to a variable, and then access it like a normal JavaScript array, but it views my array as one large text string. How do ensure that it reads it like an array?
data[0][0]
,[0][1]
,[0][2]
, etc. to access3
,2
,7
respectively. – Brad Christie Jan 15 '11 at 20:27$c
) asarray(array(3,2,7))
. But, I take full accountability for missing that they didn't specify they wanted JSON parsed (though I can't confirm if they included aheader()
in the AJAX page specifying JSON that.post
wouldn't take care of it automatically) – Brad Christie Jan 15 '11 at 20:44