duancheng3342 2014-08-26 21:06
浏览 75
已采纳

如何在PHP中将NSArray传递给索引数组?

I have a simple NSArray in Xcode which I'd like to pass to PHP file and use it as a loop. How can I do this?

For example, my NSArray looks like this:

    NSArray *array = [NSArray arrayWithObjects: @"hello", @"hi", @"how are you", nil];

What I have done so far but no success

    NSArray *array = [NSArray arrayWithObjects:@"hello", @"hi", @"how are you?", nil];
NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array
                                                       options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    NSString *postString = [NSString stringWithFormat:@"array=%@", array];
    NSData* data = [postString dataUsingEncoding:NSUTF8StringEncoding];

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate:self delegateQueue: [NSOperationQueue mainQueue]];

    NSURL* url = [NSURL URLWithString:@"http://url.com/file.php"];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                          timeoutInterval:60.0];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:data];

    __block NSString *resultString;

    NSURLSessionDataTask* dataTask = [defaultSession dataTaskWithRequest:urlRequest
                                                       completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                                                           if(error == nil)
                                                           {
                                                               resultString = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
                                                               NSLog(@"Data: %@",resultString);
                                                           }


                                                       }];
    [dataTask resume];

My PHP. I went through a tutorial here

    $array = array();
    $array = $_POST['array'];
    $arrlength = count($array);

    for ($i=1; $i <= $arrlength; $i++)
    {
        //do something
    }

I tried echoing the $array but it was empty. I know somethings about Objective-c but I have very little knowledge about PHP. My code can $_POST NSStrings but I'm still confused of how I can do this with NSArray.

If anyone could please give me some pointers or give me links on some tutorials links that will be great. I have tried searching on stackoverflow and google on how to this, but their wasn't any thing I could understand.

So my question is really simple. How can I convert NSArray to indexed arrays in PHP?

  • 写回答

2条回答 默认 最新

  • duanqun9740 2014-08-26 21:23
    关注

    So using what you have and the other question change this:

    NSString *postString = [NSString stringWithFormat:@"array=%@", array];
    

    To:

    NSMutableString *postString = [NSMutableString alloc]
    for (NSString *thing in array) {
        [postString appendFormat:@"array[]=%@&",[thing stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    }
    

    This should give you the post string you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄