views:

741

answers:

1

To pass an array from PHP to javascript via ajax, I am going to use json_encode, and recieve it on the javascript end. However, I will be returning multiple rows from my MySQL database, and need to return multiple JSON-formatted arrays. One way I thought of doing this was to string the JSON arrays together in php with some obscure character, such as a pipe character, and then separate them on the javascript end. But is there a more elegant way to do this?

Edit: this post explains what I am trying to do.

+10  A: 

Just send them as an JSON-encoded array of arrays.

<?php
$row=array('foo'=>'bar','baz'=>'quux');
echo json_encode(array($row,$row,$row,$row));
?>

Results in

[
 {"foo":"bar","baz":"quux"},
 {"foo":"bar","baz":"quux"},
 {"foo":"bar","baz":"quux"},
 {"foo":"bar","baz":"quux"}
]

This can then be processed exactly like an array on the client side.

AKX