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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器