Home > php教程 > php手册 > body text

写自己的 http_build_query

WBOY
Release: 2016-06-21 09:06:12
Original
1005 people have browsed it

我们知道, PHP的 parse_str() 函数可以将 URL Query 格式的字符串解析成关联数组, 与PHP生成 $_GET 使用的相同的策略. parse_str() 的"反函数"是 http_build_query(), 它将关联数组和对象生成 URL Query 字符串. 不过, 只在PHP5之后才被支持. 所以, 我们需要编写自己的 http_build_query()

function my_http_build_query($data){ $str = ''; foreach($data as $key=>$value){  foreach(child_str($value) as $v){   $str .= "$key$v&";  } } return substr($str, 0, strlen($str)-1);}function child_str($data){ $str = array(); if(is_array($data)){  foreach($data as $key=>$value){   foreach(child_str($value) as $v){    $str[] = "[$key]$v";   }  } }else{  // do url encoding here  $data = str_replace('&', '%26', '='.$data);  $str[] = $data; } return $str;}



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template