Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON_解码二维对象数组?_Php_Javascript_Ajax_Post - Fatal编程技术网

Php JSON_解码二维对象数组?

Php JSON_解码二维对象数组?,php,javascript,ajax,post,Php,Javascript,Ajax,Post,我有一个二维对象数组,如下所示: function test(n){ this.id = n; } var testArray= new Array(2); for(i = 0; i < testArray.length; i++){ testArray[i] = new Array(2); for(j = 0; j < testArray[i].length; j++){ testArray[i][j] = new test((2*i)+j); } }

我有一个二维对象数组,如下所示:

function test(n){
  this.id = n;
}

var testArray= new Array(2);
for(i = 0; i < testArray.length; i++){
  testArray[i] = new Array(2);
  for(j = 0; j < testArray[i].length; j++){
    testArray[i][j] = new test((2*i)+j);
  }
}
执行jquery AJAX调用后:

$.post("test.php",text,function(data){
  alert(data);
});
我不知道如何在PHP端解码和使用此对象,到目前为止,我已经尝试过类似的方法:

<?php 

$data = json_decode($_POST);
if($data == null){
    echo "fail";
} else {
    echo $data; 
} 

?>
$.post("test.php", data, function(data) {
$data = $_POST['testA'];
然后错误不会出现,但它总是输出“失败”


有人知道我需要在PHP端做什么才能访问数据吗?

为什么要在上面运行stringify?如果您只是这样发送:

<?php 

$data = json_decode($_POST);
if($data == null){
    echo "fail";
} else {
    echo $data; 
} 

?>
$.post("test.php", data, function(data) {
$data = $_POST['testA'];
您应该能够像这样检索它:

<?php 

$data = json_decode($_POST);
if($data == null){
    echo "fail";
} else {
    echo $data; 
} 

?>
$.post("test.php", data, function(data) {
$data = $_POST['testA'];

我只是尝试直接发布数据,当我调用$data=json_decode($_POST['testA']);我仍然得到一个错误,它说它需要一个字符串。我的答案中没有json解码,因为这样,$\u POST['testA']已经是一个数组了。啊,我的错误,好了,它现在可以工作了。非常感谢:)我知道我不需要对数据进行字符串化,因为我自己已经手动将其转换为JSON格式。那么我以前是在串化JSON对象吗?不,你现在根本不用JSON,因为这是不必要的。哦,好吧,我现在明白了。我不知道我需要做什么来跨服务器传输数据,我认为需要JSON。我想这就是我困惑的地方。