php implode() 二维数组的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11038282/
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
Easiest way to implode() a two-dimensional array?
提问by SomeKittens
I'm new to PHP, and don't have quite the grip on how it works. If I have a two dimensional array as such (returned by a database):
我是 PHP 的新手,对它的工作原理不太了解。如果我有一个二维数组(由数据库返回):
array(3) {
[0]=> array(1) {
["tag_id"]=> string(1) "5"
}
[1]=> array(1) {
["tag_id"]=> string(1) "3"
}
[2]=> array(1) {
["tag_id"]=> string(1) "4"
}
}
and want to turn it into the string 5,3,4what would be the quickest way do do this? I currently have an obnoxious foreachloop, but was hoping it could be done in one line. A standard implodegives me Array,Array,Array.
并想把它变成字符串5,3,4什么是最快的方法呢?我目前有一个令人讨厌的foreach循环,但希望它可以在一行中完成。一个标准implode给了我Array,Array,Array。
回答by Paul
This modifies your array using array_map, but probably for the better by turning it into a 1D array of tag_id's. Then you can just use implode like normal:
这会使用array_map修改您的数组,但可能通过将其转换为tag_id's 的一维数组来改善您的数组。然后你可以像往常一样使用内爆:
$arr = array_map(function($el){ return $el['tag_id']; }, $arr);
$str = implode(',', $arr);
If you don't want to modify your array than you can just do this:
如果您不想修改数组,则可以这样做:
$str = implode(',', array_map(function($el){ return $el['tag_id']; }, $arr));
回答by Bruce Lim
You asked for a two-dimensional array, here's a function that will work for multidimensional array.
您要求一个二维数组,这是一个适用于多维数组的函数。
function implode_r($g, $p) {
return is_array($p) ?
implode($g, array_map(__FUNCTION__, array_fill(0, count($p), $g), $p)) :
$p;
}
I can flatten an array structure like so:
我可以像这样展平一个数组结构:
$multidimensional_array = array(
'This',
array(
'is',
array(
'a',
'test'
),
array(
'for',
'multidimensional',
array(
'array'
)
)
)
);
echo implode_r(',', $multidimensional_array);
The results is:
结果是:
This,is,a,test,for,multidimensional,array
回答by Scot Nery
// simplest
$str = implode(',',array_map('implode',$arr));
回答by apokryfos
If one simply wants to implode a single array "column" as in this case, then the simplest thing to do is:
如果在这种情况下只是想内爆单个数组“列”,那么最简单的方法是:
implode(",", array_column($array,"tag_id"));
回答by Loc Nguyen
One line command
一行命令
implode(',', array_map('implode', $arr, array_fill(0, count($arr), '')))
回答by hannebaumsaway
Check out the below from the PHP implode() manual:
从PHP implode() 手册中查看以下内容:
<?php
/**
* Implode an array with the key and value pair giving
* a glue, a separator between pairs and the array
* to implode.
* @param string $glue The glue between key and value
* @param string $separator Separator between pairs
* @param array $array The array to implode
* @return string The imploded array
*/
function array_implode( $glue, $separator, $array ) {
if ( ! is_array( $array ) ) return $array;
$string = array();
foreach ( $array as $key => $val ) {
if ( is_array( $val ) )
$val = implode( ',', $val );
$string[] = "{$key}{$glue}{$val}";
}
return implode( $separator, $string );
}
?>
If you only want to return the value (and not the key), just modify the above to use $string[] = "{$val}";.
如果你只想返回值(而不是键),只需修改上面的使用$string[] = "{$val}";.
回答by RafaSashi
function recursive_implode($connector=',', $array=[], $implod='keys'){
if($implod=='keys'){
$results=implode($connector,array_keys($array));
}
else{
$results=implode($connector,$array);
}
foreach($array as $key=> $value){
if(is_array($value)){
$results.=$connector.recursive_implode($connector,$value,$implod);
}
}
return $results;
}

