有没有办法将多个数组传递给 PHP json_encode 并用 jQuery 解析它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8373315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:33:43  来源:igfitidea点击:

Is there a way to pass multiple arrays to PHP json_encode and parse it with jQuery?

phpjqueryajaxjson

提问by chromedude

Right now I have this PHP:

现在我有这个 PHP:

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

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

这是通过 JQuery 的 AJAX GET 请求发送的。

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?

我希望能够发送多个数组,json_encode()有没有办法做到这一点,你将如何用 jQuery 解析它?

回答by Riyono

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

当然,你可以发送一个数组数组。PHP 关联数组将成为一个 javascript 对象。

In PHP:

在 PHP 中:

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

and then on jQuery

然后在 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

回答by zb'

The code should be like the following:

代码应如下所示:

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

in jQuery use

在 jQuery 中使用

var columns_array=jQuery.parseJSON(response);
columns=columns_array[0];
columns1=columns_array[1];

回答by learner

  $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.....

回答by uutsav

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

在您填写的所有阵列,即$array1_json$array2_json等在我的情况,

$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:

在您的 .js 文件中,使用:

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
}