dsyo9700 2014-06-20 13:51
浏览 36
已采纳

如何解析网址的数组元素

I have a url which looks like this:

http://localhost/projectcode12may2014/ampanel/index.php?rel=common_listing&module=company&field%5B%5D=address&adv_operation%5B%5D=c&value%5B%5D=sector&query_type%5B%5D=AND&submit=Submit

In the decoded form, it looks like:

http://localhost/projectcode12may2014/ampanel/index.php?rel=common_listing&module=company&field[]=address&adv_operation[]=c&value[]=sector&query_type[]=AND&submit=Submit

I am trying to parse this URL and get the values of field[], adv_operation[] and query_type[] as arrays, but I am just getting Array written in text if I try to parse these fields. I am using parse_url() and parse_str() for parsing. Can anyone suggest a suitable method for this? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongshuming7131 2014-06-20 13:58
    关注

    You're probably displaying the array section incorrectly, because your method should work.

    You use parse_url to extract the "query" segment of the URL, then use parse_str to load it into a variable.

    Example:

    $url = "http://example.com/page.php?a=apple&b=banana&z=zebra&arr[]=1&arr[]=2";
    $query = parse_url($url, PHP_URL_QUERY);
    
    var_dump($query);
    // string(40) "a=apple&b=banana&z=zebra&arr[]=1&arr[]=2"
    
    parse_str($query, $parsed);
    var_dump($parsed);
    /*
    array(4) {
      ["a"]=>
      string(5) "apple"
      ["b"]=>
      string(6) "banana"
      ["z"]=>
      string(5) "zebra"
      ["arr"]=>
      array(2) {
        [0]=>
        string(1) "1"
        [1]=>
        string(1) "2"
      }
    }
    */
    
    // $parsed["arr"] is now array(1, 2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法