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条)

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号