TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Return Multiples Array in JavaScript from PHP

In php I return this from a function:

return json_encode(array($array1, $array2, $array3, $array4));  

Each array contains single elements, loaded lke this:

while ($obj = $DB->next($res)) {
    $array1[] = $obj->the_data;
}

Now in JavaScript, I alert data[0], it shows me "["

Of course the entire data looks similar to this:

[["element1", "element2", "element3"],["element1", "element2", "element2" ... etc

Am I loading the array's incorrectly in php? or am I parsing them incorrectly in JavaScript?

Upvotes: 0

Views: 25

Answers (1)

Quentin
Quentin

Reputation: 943220

It looks like the data is a string instead of an array. This suggests that you aren't decoding the JSON.

jQuery will do that automatically if the response from the server is marked as JSON.

By default PHP marks responses as HTML, you need to explicitly say you are returning JSON:

header("Content-Type: application/json");

Upvotes: 4

Related Questions