我正在寻找一种将关联数组转换为字符串的快速方法。典型的结构类似于 URL 查询字符串,但具有可自定义的分隔符,因此我可以将 '&
' 用于 xhtml 链接或使用 '&
' 否则。
我的第一个倾向是使用 foreach
但由于我的方法可能在一个请求中被多次调用,我担心它可能太慢了。
<?php
$Amp = $IsXhtml ? '&' : '&';
$Parameters = array('Action' => 'ShowList', 'Page' => '2');
$QueryString = '';
foreach ($Parameters as $Key => $Value)
$QueryString .= $Amp . $Key . '=' . $Value;
有没有更快的方法?
最佳答案
您可以使用 http_build_query()
这样做。
Generates a URL-encoded query string from the associative (or indexed) array provided.
关于php - 用键内爆关联数组的最快方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/408032/