http_build_query
http_build_query — 生成 URL-encode 之后的请求字符串
string http_build_query ( mixed $query_data
[, string $numeric_prefix
[, string $arg_separator
[, int $enc_type
= PHP_QUERY_RFC1738
]]] )
Note:只有基础数组中的数字下标元素才获取了前缀,其它数字下标元素则不需要为了合法的变量名而加上前缀。
注意,http_build_query() Returns a URL-encoded string.
(今天在做一个接口的时候,发现重复编码2次,一直找不到问题...后来发现其中一次是在此处...)
mixed $query_data:object
<?php class parentClass { public $pub = 'publicParent'; protected $prot = 'protectedParent'; private $priv = 'privateParent'; public $pub_bar = Null; protected $prot_bar = Null; private $priv_bar = Null; public function __construct(){ $this->pub_bar = new childClass(); $this->prot_bar = new childClass(); $this->priv_bar = new childClass(); } } class childClass { public $pub = 'publicChild'; protected $prot = 'protectedChild'; private $priv = 'privateChild'; } $parent = new parentClass(); echo '$parent->pub = ' . $parent->pub; echo '<br>'; echo '$parent->pub_bar->pub = ' . $parent->pub_bar->pub; echo '<br>'; echo urldecode(http_build_query($parent)); ?>
//结果 权限问题,最终打印出的是public的内容。
$parent->pub = publicParent
$parent->pub_bar->pub = publicChild
pub=publicParent&pub_bar[pub]=publicChild