将PHP数组的key = value [英] Convert php array to key=value
本文介绍了将PHP数组的key = value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有PHP函数转换阵列
来像的key = value
在HTML中,如果不是有什么最佳做法?
输入
$ htmlOptions =阵列(类= GT;'容器');
...
< DIV< PHP someFunction($ htmlOptions); ?> >< / DIV>
输出
< DIV CLASS =容器>< / DIV>
解决方案
这应该是罚款:
函数printAttributes($数组){
$ attrArray =阵列();
的foreach($数组作为$名=> $值){
$ attrArray [] = $名称。 '='。$价值。'';
} 返回加入('',$ attrArray);
}//(...)$ htmlOptions =阵列(类= GT;'容器');
然后在 HTML
< DIV< = printAttributes($ htmlOptions); ?>>< / DIV>
Is there php function to convert Array
to be like key=value
in html, and if not what's the best practices ?
Input
$htmlOptions = array('class'=>'container');
...
<div <?php someFunction($htmlOptions); ?> ></div>
Output
<div class="container"></div>
解决方案
This should be fine:
function printAttributes($array) {
$attrArray = array();
foreach ($array as $name => $value) {
$attrArray[] = $name. '="' . $value . '"';
}
return join(' ', $attrArray);
}
// (...)
$htmlOptions = array('class'=>'container');
And then in HTML:
<div <?= printAttributes($htmlOptions); ?>></div>
这篇关于将PHP数组的key = value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文