简体   繁体   English

如何将多维数组发布到PHP

[英]How to POST multi-dimensional array to PHP

I have an array of arrays of dictionaries. 我有一个字典数组数组。 Example MainArray SubArray1 Dict 1 Dict 2 SubArray2 Dict 1 Dict 2 示例MainArray SubArray1 Dict 1 Dict 2 SubArray2 Dict 1 Dict 2

Here is the code before I send an NSMutableUrlRequest using the string output. 这是在我使用字符串输出发送NSMutableUrlRequest之前的代码。

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:itemListArray
                                                   options:kNilOptions error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

Which then goes to 然后去

NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];

An NSUrlConnection follows. 接下来是NSUrlConnection。

The jsonstring output is [[{"Description":"Item1"},{"Description":"Item2"}],[{"Description":"SItem1"},{"Description":"SItem2"}]] jsonstring输出为[[{“ Description”:“ Item1”},{“ Description”:“ Item2”}],[{“ Description”:“ SItem1”},{“ Description”:“ SItem2”]]]]

My PHP code is pretty simple and returns the jsonstring as above. 我的PHP代码非常简单,并且如上所述返回jsonstring。

$data1 = $_POST["jsonstring"];
var_dump($data1);

My issue now is I don't know how to separate the arrays. 我现在的问题是我不知道如何分离阵列。 Do I have to set up some string formatting to separate the data? 我是否需要设置一些字符串格式来分隔数据? For example, pull all data between each set of brackets []. 例如,在每组方括号[]之间拉出所有数据。 Then further separate data between all ""? 然后在所有“”之间进一步分开数据?

Is there an easier way to post a multi-dimensional array of dictionaries? 有没有更简单的方法来发布多维数组的字典?

$data1 = '[[{"Description":"Item1"},{"Description":"Item2"}],[{"Description":"SItem1"},{"Description":"SItem2"}]]';

var_dump(json_decode($data1, true));

or without the true as second parameter to allow objects instead of converting them to arrays 或不使用true作为第二个参数来允许对象而不是将它们转换为数组

var_dump(json_decode($data1));

OUTPUT: 输出:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    array(1) {
      ["Description"]=>
      string(5) "Item1"
    }
    [1]=>
    array(1) {
      ["Description"]=>
      string(5) "Item2"
    }
  }
  [1]=>
  array(2) {
    [0]=>
    array(1) {
      ["Description"]=>
      string(6) "SItem1"
    }
    [1]=>
    array(1) {
      ["Description"]=>
      string(6) "SItem2"
    }
  }
}

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

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