在 PHP 中内爆关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6556985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Imploding an associative array in PHP
提问by Alec
Say I have an array:
说我有一个数组:
$array = Array(
'foo' => 5,
'bar' => 12,
'baz' => 8
);
And I'd like to print a line of text in my view like this:
我想在我的视图中打印一行文本,如下所示:
"The values are: foo (5), bar (12), baz (8)"
“值是:foo (5), bar (12), baz (8)”
What I could do is this:
我能做的是:
$list = Array();
foreach ($array as $key => $value) {
$list[] = "$key ($value)";
}
echo 'The values are: '.implode(', ',$list);
But I feel like there should be an easier way, without having to create the $list
array as an extra step. I've been trying array_map
and array_walk
, but no success.
但我觉得应该有一种更简单的方法,而不必将创建$list
数组作为额外的步骤。我一直在尝试array_map
和array_walk
,但没有成功。
So my question is: what's the best and shortest way of doing this?
所以我的问题是:最好和最短的方法是什么?
回答by linepogl
The problem with array_map
is that the callback function does not accept the key as an argument. You could write your own function to fill the gap here:
问题array_map
在于回调函数不接受键作为参数。您可以编写自己的函数来填补此处的空白:
function array_map_assoc( $callback , $array ){
$r = array();
foreach ($array as $key=>$value)
$r[$key] = $callback($key,$value);
return $r;
}
Now you can do that:
现在你可以这样做:
echo implode(',',array_map_assoc(function($k,$v){return "$k ($v)";},$array));
回答by Roberto Santana
For me the best and simplest solution is this:
对我来说,最好和最简单的解决方案是:
$string = http_build_query($array, '', ',');
回答by Lightness Races in Orbit
There isa way, but it's pretty verbose(and possiblyless efficient):
有是一种方式,但它是相当冗长(和可能效率较低):
<?php
$array = Array(
'foo' => 5,
'bar' => 12,
'baz' => 8
);
// pre-5.3:
echo 'The values are: '. implode(', ', array_map(
create_function('$k,$v', 'return "$k ($v)";'),
array_keys($array),
array_values($array)
));
echo "\n";
// 5.3:
echo 'The values are: '. implode(', ', array_map(
function ($k, $v) { return "$k ($v)"; },
array_keys($array),
array_values($array)
));
?>
Your original code looks fine to me.
你的原始代码对我来说很好。
回答by Christopher Armstrong
You could print out the values as you iterate:
您可以在迭代时打印出这些值:
echo 'The values are: ';
foreach ($array as $key => $value) {
$result .= "$key ($value),";
}
echo rtrim($result,',');
回答by Divyang
Taking help from the answer of @linepogl, I edited the code to make it more simple, and it works fine.
从@linepogl 的回答中获得帮助,我编辑了代码以使其更简单,并且运行良好。
function array_map_assoc($array){
$r = array();
foreach ($array as $key=>$value)
$r[$key] = "$key ($value)";
return $r;
}
And then, just call the function using
然后,只需使用调用该函数
echo implode(' , ', array_map_assoc($array));
回答by Fran Díaz
Clearly, the solution proposed by Roberto Santanaits the most usefull. Only one appointmen, if you want to use it for parse html attributes (for example data), you need double quotation. This is an approach:
显然,Roberto Santana提出的解决方案是最有用的。只有一个任命,如果你想用它来解析html属性(例如数据),你需要双引号。这是一种方法:
Example: var_dump( '<td data-'.urldecode( http_build_query( ['value1'=>'"1"','value2'=>'2' ], '', ' data-' ) ).'></td>');
Output: string '<td data-value1="1" data-value2=2></td>' (length=39)
回答by Minis
I came up with
我想出了
foreach ($array as &$item)
{
$item = reset($item);
}
$array = implode(', ', $array);
var_dump($array);