用键内爆关联数组的最快方法 [英] Fastest way to implode an associative array with keys
本文介绍了用键内爆关联数组的最快方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种将关联数组转换为字符串的快速方法.典型的结构类似于 URL 查询字符串,但具有可自定义的分隔符,因此我可以将 '&
' 用于 xhtml 链接或 '&
' 否则.
I'm looking for a fast way to turn an associative array in to a string. Typical structure would be like a URL query string but with customizable separators so I can use '&
' for xhtml links or '&
' otherwise.
我的第一个倾向是使用 foreach
但由于我的方法可以在一个请求中多次调用,我担心它可能太慢了.
My first inclination is to use foreach
but since my method could be called many times in one request I fear it might be too slow.
<?php
$Amp = $IsXhtml ? '&' : '&';
$Parameters = array('Action' => 'ShowList', 'Page' => '2');
$QueryString = '';
foreach ($Parameters as $Key => $Value)
$QueryString .= $Amp . $Key . '=' . $Value;
有没有更快的方法?
推荐答案
您可以使用 http_build_query()
做到这一点.
You can use http_build_query()
to do that.
从提供的关联(或索引)数组生成 URL 编码的查询字符串.
Generates a URL-encoded query string from the associative (or indexed) array provided.
这篇关于用键内爆关联数组的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文