简体   繁体   English

通过Ajax发布发送JavaScript多维数组

[英]Send javascript multidimensional array via ajax post

I have a multi-dimensional array like below 我有一个如下的多维数组

var myarray = new Array();
myarray["firstkey"] = new Array();
myarray["firstkey"]["secondkey"] = new Array();
myarray["firstkey"]["secondkey"]["0"] = new Array();
myarray["firstkey"]["secondkey"]["0"]["a"] = 100;
myarray["firstkey"]["secondkey"]["0"]["b"] = 1200;
myarray["firstkey"]["secondkey"]["0"]["c"] = 32000;
myarray["firstkey"]["secondkey"]["0"]["d"] = 23001;
myarray["firstkey"]["secondkey"]["0"]["e"] = "text";
myarray["firstkey"]["secondkey"]["0"]["f"] = "text";
myarray["firstkey"]["secondkey"]["0"]["g"] = "text";
myarray["firstkey"]["secondkey"]["0"]["h"] = "";
myarray["firstkey"]["secondkey"]["0"]["i"] = 0;
myarray["firstkey"]["secondkey"]["0"]["aa"] = new Array();
myarray["firstkey"]["secondkey"]["0"]["bb"]["bbb"] = 16;
myarray["firstkey"]["secondkey"]["0"]["cc"]["ccc"] = "text";
myarray["firstkey"]["secondkey"]["0"]["dd"]["ddd"] = new Array();
myarray["firstkey"]["secondkey"]["0"]["ee"]["eee"]["0"] = "text";
myarray["firstkey"]["secondkey"]["0"]["ff"]["fff"]["1"] = "text";`

and I want to get it from PHP, but I can't find solution. 并且我想从PHP中获取它,但是找不到解决方案。 Can anybody please tell me the solution. 有人可以告诉我解决方案吗? The response would be highly appreciated. 对此表示高度赞赏。

Maybe you can try sending JSON? 也许您可以尝试发送JSON?

You should read it from PHP with: 您应该使用以下方法从PHP阅读:

$json = file_get_contents("php://input");

You could do AJAX 你可以做AJAX

The JavaScript: JavaScript:

var array = JSON.stringify(myarray);
$.ajax({
    url: "/myphp/file.php",
    data: {array: array},
    dataType: "json",
    success: function(data){
        alert(data["return"]);
    }
});

The PHP: PHP:

$array = json_decode($_POST["array"]);
$savedVal = $array["firstkey"]["secondkey"]["0"]["a"];
echo json_encode(array("return" => $savedVal));

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

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