内爆带有按键的关联数组最快的方法 [英] Fastest way to implode an associative array with keys

查看:173
本文介绍了内爆带有按键的关联数组最快的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一个快速的方式把一个关联数组中的字符串。典型的结构会像一个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 ? '&amp;' : '&';
$Parameters = array('Action' => 'ShowList', 'Page' => '2');
$QueryString = '';
foreach ($Parameters as $Key => $Value)
        $QueryString .= $Amp . $Key . '=' . $Value;

有一个更快的方法?

Is there a faster way?

推荐答案

您可以使用 http_build_query() 来做到这一点。

You can use http_build_query() to do that.

从关联生成一个URL-CN codeD查询字符串(或索引)阵列提供。

Generates a URL-encoded query string from the associative (or indexed) array provided.

这篇关于内爆带有按键的关联数组最快的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆