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?

1
  • PHP & Javascript both support arrays-of-arrays, so just embed your two arrays inside a parent array and send the parent over. Commented Dec 4, 2011 at 5:08

4 Answers 4

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
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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];

1 Comment

also, there some problems with transfering special chars like quotes in browsers i also like to wrap base64 around response
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.....

Comments

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.