Php 使用关键点内爆关联数组的最快方法
我正在寻找一种将关联数组转换为字符串的快速方法。典型的结构类似于URL查询字符串,但带有可自定义的分隔符,因此我可以使用“Php 使用关键点内爆关联数组的最快方法,php,arrays,query-string,associative-array,implode,Php,Arrays,Query String,Associative Array,Implode,我正在寻找一种将关联数组转换为字符串的快速方法。典型的结构类似于URL查询字符串,但带有可自定义的分隔符,因此我可以使用“”&'用于xhtml链接,否则为'&' 我的第一个倾向是使用foreach,但由于我的方法可能在一个请求中被多次调用,我担心它可能太慢了 <?php $Amp = $IsXhtml ? '&' : '&'; $Parameters = array('Action' => 'ShowList', 'Page' => '2'); $
”&代码>'用于xhtml链接,否则为'&
'
我的第一个倾向是使用foreach
,但由于我的方法可能在一个请求中被多次调用,我担心它可能太慢了
<?php
$Amp = $IsXhtml ? '&' : '&';
$Parameters = array('Action' => 'ShowList', 'Page' => '2');
$QueryString = '';
foreach ($Parameters as $Key => $Value)
$QueryString .= $Amp . $Key . '=' . $Value;
您可以使用
从提供的关联(或索引)数组生成URL编码的查询字符串
顺便说一句,我在寻找最好的方法来内爆一个关联数组,但使用我自己的分离器等
因此,我使用PHP的array_walk()函数将一个关联数组加入到一个参数列表中,然后可以应用到HTML标记中
// Create Params Array
$p = Array("id"=>"blar","class"=>"myclass","onclick"=>"myJavascriptFunc()");
// Join Params
array_walk($p, create_function('&$i,$k','$i=" $k=\"$i\"";'));
$p_string = implode($p,"");
// Now use $p_string for your html tag
显然,您可以在自己的函数中以某种方式保留它,但它让您了解如何使用自己的方法连接关联数组。
希望对某人有所帮助:)这是我能想到的最基本的版本:
public function implode_key($glue = "", $pieces = array())
{
$keys = array_keys($pieces);
return implode($glue, $keys);
}
一种方法是使用print\r(array,true)
,如果您不关心确切的格式,它将返回数组的字符串表示形式,但是您确实需要一些简单的东西,但不需要print\r
的换行符,您还可以使用json\u encode($value)
快速、简单地格式化输出。(注意,它也适用于其他数据类型)
这是我的解决方案,例如div数据属性:
<?
$attributes = array(
'data-href' => 'http://example.com',
'data-width' => '300',
'data-height' => '250',
'data-type' => 'cover',
);
$dataAttributes = array_map(function($value, $key) {
return $key.'="'.$value.'"';
}, array_values($attributes), array_keys($attributes));
$dataAttributes = implode(' ', $dataAttributes);
?>
<div class="image-box" <?= $dataAttributes; ?> >
<img src="http://example.com/images/best-of.jpg" alt="">
</div>
$companys->toArray()
--
如果您的$variable
是一个对象,这只是一种情况,否则只需传递$companys即可
就这样 从简单数组创建HTML属性字符串(带引号)的一行程序:
$attrString = str_replace("+", " ", str_replace("&", "\" ", str_replace("=", "=\"", http_build_query($attrArray)))) . "\"";
例子:
使用array_walk,这种更短、更透明、更直观的方式怎么样
$attributes = array(
'data-href' => 'http://example.com',
'data-width' => '300',
'data-height' => '250',
'data-type' => 'cover',
);
$args = "";
array_walk(
$attributes,
function ($item, $key) use (&$args) {
$args .= $key ." = '" . $item . "' ";
}
);
// output: 'data-href="http://example.com" data-width="300" data-height="250" data-type="cover"
我的解决方案:
$url_string = http_build_query($your_arr);
$res = urldecode($url_string);
我自己试图在PHPAPI中找到这个方法,这绝对是一条路要走。如果不是的话,另一种选择是使用修改过的内爆方法,比如http\u build\u query()会更快。我想知道这是否是解决一般问题的最快方法。此函数中正在进行一些编码。那么,如果不是URL,这真的比array_walk快吗?如果不想对它进行编码呢?问题是http_build_query
escape special charI比较了serialize()、json_encode()和http_build_query()。http_build_query()以略低于serialize()的优势获胜,而json_encode是迄今为止速度最慢的。它还编码特殊字符,因此这不是一个好的选择。不要忘记,如果输出HTML,正确的语法是&;不&,看这是错误的是的,它可以工作,但它是无效的HTML。$request\u str=print\r($\u request,true);//展平日志文件的数组,但输出为打印格式,而不是使用提供的分隔符连接的key=value
。如果create\u函数的语法使任何人感到困惑,请参阅array\u walk比foreach慢,读取更复杂。请参阅:发现此版本作为http_build_query()编码URL的第一个解决方案非常有用。它只会内爆键,而不会使用选定的分隔符内爆key=value
!这只会将键添加到字符串中;这些值丢失了。
$attrString = str_replace("+", " ", str_replace("&", "\" ", str_replace("=", "=\"", http_build_query($attrArray)))) . "\"";
$attrArray = array("id" => "email",
"name" => "email",
"type" => "email",
"class" => "active large");
echo str_replace("+", " ", str_replace("&", "\" ", str_replace("=", "=\"", http_build_query($attrArray)))) . "\"";
// Output:
// id="email" name="email" type="email" class="active large"
$attributes = array(
'data-href' => 'http://example.com',
'data-width' => '300',
'data-height' => '250',
'data-type' => 'cover',
);
$args = "";
array_walk(
$attributes,
function ($item, $key) use (&$args) {
$args .= $key ." = '" . $item . "' ";
}
);
// output: 'data-href="http://example.com" data-width="300" data-height="250" data-type="cover"
$url_string = http_build_query($your_arr);
$res = urldecode($url_string);