I'm a novice at PHP and I need a quick solution to the following problem but can't seem to come up with one:
我是一个PHP新手,我需要一个快速的解决方案来解决下面的问题,但是我似乎不能想出一个:
I have a multi-dimensional array like so
我有一个这样的多维数组。
Array
(
[0] => Array
(
[blogTags_id] => 1
[tag_name] => google
[inserted_on] => 2013-05-22 09:51:34
[inserted_by] => 2
)
[1] => Array
(
[blogTags_id] => 2
[tag_name] => technology
[inserted_on] => 2013-05-22 09:51:34
[inserted_by] => 2
)
)
I want to use the implode()
to somehow return a comma-separated string containing values of tag_name
key like so.
我想使用内爆()来以某种方式返回一个以逗号分隔的字符串,其中包含像这样的tag_name键的值。
google, technology
Is it possible to achieve this effect with the said function? If not then please suggest an alternate solution.
是否可以用上述函数实现这个效果?如果没有,请建议另一种解决办法。
7 个解决方案
#1
127
Quite simple:
很简单:
$input = array(
array(
'tag_name' => 'google'
),
array(
'tag_name' => 'technology'
)
);
echo implode(', ', array_map(function ($entry) {
return $entry['tag_name'];
}, $input));
http://3v4l.org/ltBZ0
and new in php v5.5.0, array_column
:
在php v5.5.0中新增array_column:
echo implode(', ', array_column($input, 'tag_name'));
#2
8
join(',', array_map(function (array $tag) { return $tag['tag_name']; }, $array))
#3
8
array_map
is a call back function, where you can play with the passed array. this should work.
array_map是一个回调函数,您可以在其中使用传递的数组。这应该工作。
$str = implode(',', array_map(function($el){ return $el['tag_id']; }, $arr));
#4
5
very simple go for this
这很简单。
$str;
foreach ($arrays as $arr) {
$str .= $arr["tag_name"] . ",";
}
$str = trim($str, ',');//removes the final comma
#5
2
Although this question is related to string conversion, I stumbled upon this while wanting an easy way to write arrays to my log files. If you just want the info, and don't care about the exact cleanliness of a string you might consider:
尽管这个问题与字符串转换有关,但我无意中发现了这个问题,并希望有一种简单的方法将数组写入我的日志文件。如果你只是想要信息,而不关心你可能考虑的字符串的确切清洁度:
json_encode($array)
#6
1
If you want "tag_name" with associated "blogTags_id" use: (PHP > 5.5)
如果您希望“tag_name”与关联的“blogTags_id”关联,请使用:(PHP > 5.5)
$blogDatas = array_column($your_multi_dim_array, 'tag_name', 'blogTags_id');
echo implode(', ', array_map(function ($k, $v) { return "$k: $v"; }, array_keys($blogDatas), array_values($blogDatas)));
#7
0
In this situation implode($array,','); will works, becasue you want the values only. In PHP 5.6 working for me.
在这种情况下内爆(美元数组,',');will可以工作,因为您只需要值。在PHP 5.6中为我工作。
If you want to implode the keys and the values in one like :
blogTags_id: 1
tag_name: google
如果您想要内爆键和值,比如:blogTags_id: 1 tag_name:谷歌。
$toImplode=array();
foreach($array as $key => $value) {
$toImplode[]= "$key: $value".'<br>';
}
$imploded=implode('',$toImplode);
Sorry, I understand wrong, becasue the title "Implode data from a multi-dimensional array". Well, my answer still answer it somehow, may help someone, so will not delete it.
抱歉,我理解错了,因为标题是“多维数组内爆数据”。好吧,我的答案还是要回答,可能会帮助别人,所以不会删除它。
#1
127
Quite simple:
很简单:
$input = array(
array(
'tag_name' => 'google'
),
array(
'tag_name' => 'technology'
)
);
echo implode(', ', array_map(function ($entry) {
return $entry['tag_name'];
}, $input));
http://3v4l.org/ltBZ0
and new in php v5.5.0, array_column
:
在php v5.5.0中新增array_column:
echo implode(', ', array_column($input, 'tag_name'));
#2
8
join(',', array_map(function (array $tag) { return $tag['tag_name']; }, $array))
#3
8
array_map
is a call back function, where you can play with the passed array. this should work.
array_map是一个回调函数,您可以在其中使用传递的数组。这应该工作。
$str = implode(',', array_map(function($el){ return $el['tag_id']; }, $arr));
#4
5
very simple go for this
这很简单。
$str;
foreach ($arrays as $arr) {
$str .= $arr["tag_name"] . ",";
}
$str = trim($str, ',');//removes the final comma
#5
2
Although this question is related to string conversion, I stumbled upon this while wanting an easy way to write arrays to my log files. If you just want the info, and don't care about the exact cleanliness of a string you might consider:
尽管这个问题与字符串转换有关,但我无意中发现了这个问题,并希望有一种简单的方法将数组写入我的日志文件。如果你只是想要信息,而不关心你可能考虑的字符串的确切清洁度:
json_encode($array)
#6
1
If you want "tag_name" with associated "blogTags_id" use: (PHP > 5.5)
如果您希望“tag_name”与关联的“blogTags_id”关联,请使用:(PHP > 5.5)
$blogDatas = array_column($your_multi_dim_array, 'tag_name', 'blogTags_id');
echo implode(', ', array_map(function ($k, $v) { return "$k: $v"; }, array_keys($blogDatas), array_values($blogDatas)));
#7
0
In this situation implode($array,','); will works, becasue you want the values only. In PHP 5.6 working for me.
在这种情况下内爆(美元数组,',');will可以工作,因为您只需要值。在PHP 5.6中为我工作。
If you want to implode the keys and the values in one like :
blogTags_id: 1
tag_name: google
如果您想要内爆键和值,比如:blogTags_id: 1 tag_name:谷歌。
$toImplode=array();
foreach($array as $key => $value) {
$toImplode[]= "$key: $value".'<br>';
}
$imploded=implode('',$toImplode);
Sorry, I understand wrong, becasue the title "Implode data from a multi-dimensional array". Well, my answer still answer it somehow, may help someone, so will not delete it.
抱歉,我理解错了,因为标题是“多维数组内爆数据”。好吧,我的答案还是要回答,可能会帮助别人,所以不会删除它。