php - 用键内爆关联数组的最快方法

标签 php arrays query-string associative-array implode

我正在寻找一种将关联数组转换为字符串的快速方法。典型的结构类似于 URL 查询字符串,但具有可自定义的分隔符,因此我可以将 '&' 用于 xhtml 链接或使用 '&' 否则。

我的第一个倾向是使用 foreach 但由于我的方法可能在一个请求中被多次调用,我担心它可能太慢了。

<?php
$Amp = $IsXhtml ? '&amp;' : '&';
$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/

相关文章:

php - 像数组一样从数据库获取值

php - mysql left join or full join with fulltext search results

php - Yii db 的返回值

c++ - 类 - 用户定义的具有动态大小的智能阵列

javascript - 数组映射函数不改变元素

javascript - 如何使用PHP动态显示时间?

arrays - VBA Excel 计算特定值

angular - 我如何将查询参数传递到 Angular 应用程序中服务内的 REST API?

mod-rewrite - 模组重写 + QSA : Same querystring param in rewrite target and in querystring - Force use of target possible?

polymer - app-location 删除查询字符串参数