return a php associative array to javascript array
Posted
by Eric Sim
on Stack Overflow
See other posts from Stack Overflow
or by Eric Sim
Published on 2010-05-07T12:04:37Z
Indexed on
2010/05/07
12:08 UTC
Read the original article
Hit count: 278
associative
|array
I am trying to return a php associative array to javascript array through ajaxRequest.responseText
Here's what I do.
First in php, I do this:
$encoded = json_encode($thisarray);
echo $encoded;
If I echo $encoded, I get {"a":"apple,arrow","b":"boy,bank","c":"cat,camp"}
Then in js script,
thisarray = new Array();
thisarray = ajaxRequest.responseText;
If I alert thisarray, I get {"a":"apple,arrow","b":"boy,bank","c":"cat,camp"}
That's wrong since alerting an array should give error. But in this case, when I alert thisarray, I get the full array!!
Needless to say, I can't call my value out of thisarray, since it is yet defined as an array.
Anyone can tell me what am I missing here?
© Stack Overflow or respective owner