繁体   English   中英

PHP读取Javascript数组

[英]PHP read Javascript array

我将数组从Javascript传递到PHP页面,如下所示。

var arrF1 = [{"Item":"item1no",
      "Desc":"item1desc",
      "Remarks":"item1note"},
             {"Item":"item2no",
      "Desc":"item2desc",
      "Remarks":"item2note"}
    ];
$.ajax({        
   type: "POST",
   url: "http://www.mydomain.com/pdfs/flist.php",
   data: { favArray : arrF1 },
   success: function() {
        alert('ok, sent');
   }
}); 

在我的PHP页面中,我读取了如下数组。

$fArray = json_decode($_POST['favArray'])

我试图像这样访问数组值。

$fArrav[0]->{'Item'}
$fArrav[0]->{'Desc'}
$fArrav[1]->{'Item'}

它是否正确? 我正在使用FPDF在服务器上生成PDF。 但有了上述内容,它还没有读取数组。

我一定不能这样做。 请帮忙。

谢谢。

有了这个PHP:

function handlePost() {
    $a = $_POST['favArray'];
    var_dump($a);
}

..和这个Javascript:

function post() {
    var a1 = [ 
        {"Item":"109249383",
         "Desc":"item1desc",
         "Remarks":"item1note"},

        {"Item":"298298210",
         "Desc":"item2desc",
         "Remarks":"item2note"}
    ];

    $.ajax({
        type: "POST",
        url: "readArray.php",
        data: { favArray : a1 },
        success: function() {
            alert1('ok, sent');
        }
    });
}

...我得到这个输出:

HTTP/1.1 200 OK
Content-Type: text/html
Server: Microsoft-IIS/7.5
X-Powered-By: PHP/5.3.10
Date: Wed, 11 Jul 2012 21:04:16 GMT
Content-Length: 315

array(2) {
  [0]=>
  array(3) {
    ["Item"]=>
    string(9) "109249383"
    ["Desc"]=>
    string(9) "item1desc"
    ["Remarks"]=>
    string(9) "item1note"
  }
  [1]=>
  array(3) {
    ["Item"]=>
    string(9) "298298210"
    ["Desc"]=>
    string(9) "item2desc"
    ["Remarks"]=>
    string(9) "item2note"
  }
}

在这种情况下,线上数据的编码不是JSON 根据jQuery的ajax函数使用的默认值,它是'application / x-www-form-urlencoded'。 看起来像这样:

favArray=%5B%7B%22Item%22%3A%22109249383%22%2C%22Desc%22%3A%22item1desc%22%2C
    %22Remarks%22%3A%22item1note%22%7D%2C%7B%22Item%22%3A%22298298210%22%2C%22
    Desc%22%3A%22item2desc%22%2C%22Remarks%22%3A%22item2note%22%7D%5D

(全部在一条线上)
因此,在PHP中调用json_decode是没有意义的 - 从来没有涉及任何JSON。 PHP自动解码url编码的帖子体。


如果使用JSON进行编码,则可以直接使用JSON.stringify() 可能需要浏览器端的json2.js (见这个答案

要使用JSON,那么在浏览器中需要这样的东西:

function post_json_encoded() {
    $.ajax({
        type: "POST",
        url: "postArray.php",
        contentType: 'application/json', // outbound header
        dataType: 'text',                // expected response
        data: JSON.stringify(a1),        // explicitly encode
        success: function() {
            alert1('ok, json sent');
        }
    });
}

...然后在php端这样的事情:

function handlePost() {
    header('Content-Type: text/plain; charset="UTF-8"');
    $post_body = file_get_contents('php://input');
    $a = json_decode($post_body);
    var_dump($a);
}

在这种情况下,线上表示如下所示:

[{"Item":"109249383","Desc":"item1desc","Remarks":"item1note"},
 {"Item":"298298210","Desc":"item2desc","Remarks":"item2note"}]

...而php var_dump输出是这样的:

array(2) {
  [0]=>
  object(stdClass)#1 (3) {
    ["Item"]=>
    string(9) "109249383"
    ["Desc"]=>
    string(9) "item1desc"
    ["Remarks"]=>
    string(9) "item1note"
  }
  [1]=>
  object(stdClass)#2 (3) {
    ["Item"]=>
    string(9) "298298210"
    ["Desc"]=>
    string(9) "item2desc"
    ["Remarks"]=>
    string(9) "item2note"
  }
}
$fArray = $_POST['favArray'];
echo $fArray[0]['Item']; // item1no
echo $fArray[1]['Item']; // item2no

不需要额外的括号:

$fArrav[0]->Item

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM