Dean219 Dean219 - 1 month ago 5x
PHP Question

Issue with encoded array from php

Im having an issue returning a value from php to javascript. I have encoded the php array like so :

echo json_encode($myArray);

And on the javascript side i do this within the $.ajax method:

success:function (data) {
alert( data);

This works and it alerts the returned array, however when i try to then set my javascript array to the value of data :

success:function (data) {
myArray = data;

This completely breaks my looping operation and so instead of printing out for example:

This is a test

It will print:


and the length of the array rather than being 4, for 4 words it is 16+ including the square brackets etc. How can i reuse the json encoded array once it has been recieved by javascript and maintain its structure?


Related: Parse JSON in JavaScript?

What you are looking for:

myArray = JSON.parse(data);