php 从多维数组内爆数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16710800/
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
Implode data from a multi-dimensional array
提问by CobaltBabyBear
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.
我想使用implode()
以某种方式返回一个逗号分隔的字符串,其中包含tag_name
像这样的键值。
google, technology
Is it possible to achieve this effect with the said function? If not then please suggest an alternate solution.
用上述功能能达到这个效果吗?如果没有,请提出替代解决方案。
回答by Yoshi
Quite simple:
非常简单:
$input = array(
array(
'tag_name' => 'google'
),
array(
'tag_name' => 'technology'
)
);
echo implode(', ', array_map(function ($entry) {
return $entry['tag_name'];
}, $input));
and new in php v5.5.0, array_column
:
新在PHP V5.5.0, array_column
:
echo implode(', ', array_column($input, 'tag_name'));
回答by Msencenb
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)
回答by Farhan
回答by deceze
join(',', array_map(function (array $tag) { return $tag['tag_name']; }, $array))
回答by Vigneswaran S
very simple go for this
非常简单的去做这个
$str;
foreach ($arrays as $arr) {
$str .= $arr["tag_name"] . ",";
}
$str = trim($str, ',');//removes the final comma
回答by Tarik
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)));
回答by Atanas Atanasov
In this situation implode($array,','); will works, becasue you want the values only. In PHP 5.6 working for me.
在这种情况下 implode($array,','); 会起作用,因为您只需要这些值。在 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
: google
$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.
抱歉,我理解错了,因为标题是“从多维数组中内爆数据”。好吧,我的答案仍然以某种方式回答它,可能会帮助某人,因此不会删除它。