18

Right now I have this PHP:

$columns = array(*/Data*/);
echo json_encode($columns);

And this is sent through an AJAX GET request with JQuery.

var columns = jQuery.parseJSON(response);

I would like to be able to send more than one array in the json_encode() is there any way to do this and how would you parse it with jQuery?

chromedude
  • 4,246
  • 16
  • 65
  • 96
  • PHP & Javascript both support arrays-of-arrays, so just embed your two arrays inside a parent array and send the parent over. – Marc B Dec 04 '11 at 05:08

4 Answers4

84

Sure, you could send an array of array. PHP associative array will become a javascript object.

In PHP:

$data = array();
$data['fruits'] = array('apple','banana','cherry');
$data['animals'] = array('dog', 'elephant');
echo json_encode($data);

and then on jQuery

var data = jQuery.parseJSON(response);

then you could then do something like this to access the values

console.log(data.fruits[0]); // apple
console.log(data.animals[1]); // elephant
Andronicus
  • 1,389
  • 11
  • 13
  • 4
    Clear concise answer. But no need to incur the overhead of jQuery on the JavaScript side. `JSON.parse(response)` will do it in plain JavaScript. – Velojet Jun 26 '14 at 21:50
9

The code should be like the following:

$columns = array(/*Data*/);
$columns1 = array(/*Data1*/);
echo json_encode(array($columns,$columns1));

in jQuery use

var columns_array=jQuery.parseJSON(response);
columns=columns_array[0];
columns1=columns_array[1];
zb'
  • 8,071
  • 4
  • 41
  • 68
  • also, there some problems with transfering special chars like quotes in browsers i also like to wrap base64 around response – zb' Dec 04 '11 at 05:07
6
  $data1 = array();
  $data2 = array();
  $data1[] = array('apple','banana','cherry');
  $data2[] = array('dog', 'elephant');
  echo json_encode(array($data1,$data2));

in ajax,

      console.log(response[0][0])//apple
      console.log(response[1][0])//dog.....
learner
  • 63
  • 1
  • 4
1

After you have populated all the arrays namely $array1_json, $array2_json etc in my case,

$number_of_array1elements = count($array1_json);
$number_of_array2elements = count($array2_json);
$number_of_array3elements = count($array3_json);

array_unshift($array1_json , $number_of_array1elements); 
// pushes element to the start of array1_json
array_unshift($array2_json , $number_of_array2elements);
array_unshift($array3_json , $number_of_array3elements);

and similarly for other arrays.

echo json_encode( array_merge($array1_json, $array2_json, $array3_json) );

In your .js file, use:

var val = xmlhttp.responseText;
var jsonData = JSON.parse(val);
var number_of_array1elements = jsonData[0];
for (var i = 1; i <= number_of_array1elements; i++ ) 
{
    // use jsonData[i] to select the required element and do whatever is needed with it
}
var number_of_array2elements = jsonData[i];
for ( i = i+1; i <= number_of_array1elements+number_of_array2elements+1; i++ ) 
{
     // use jsonData[i] to select the required element and do whatever is needed with it
}
uutsav
  • 389
  • 5
  • 16