从多维数组返回单列 [英] Return single column from a multi-dimensional array

查看:125
本文介绍了从多维数组返回单列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,我需要快速解决以下问题,但似乎无法提出一个解决方案:

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:

我有一个像这样的多维数组

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
        )
)

我想使用 implode()以某种方式返回一个逗号分隔的字符串,其中包含 tag_name 键的值.

I want to use the implode() to somehow return a comma-separated string containing values of tag_name key like so.

google, technology

是否可以通过上述功能实现此效果?如果没有,请提出替代解决方案.

Is it possible to achieve this effect with the said function? If not then please suggest an alternate solution.

推荐答案

非常简单:

$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

和php v5.5.0中的新功能, array_column :

and new in php v5.5.0, array_column:

echo implode(', ', array_column($input, 'tag_name'));

这篇关于从多维数组返回单列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆