dsdxlibt98300 2013-05-23 09:54
浏览 37
已采纳

从多维数组中内爆数据

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

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.

  • 写回答

7条回答 默认 最新

  • dongzhao5970 2013-05-23 09:57
    关注

    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:

    echo implode(', ', array_column($input, 'tag_name'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?