dqbjvg2518 2017-09-25 19:09
浏览 32
已采纳

使用PHP合并子数组

I have this array in PHP.

$all = array(
array(
    'titulo' => 'Nome 1',
    'itens' => array( 'item1', 'item2', 'item3')
),
array(
    'titulo' => 'Nome 2',
    'itens' => array( 'item4', 'item5', 'item6')
),
array(
    'titulo' => 'Nome 4',
    'itens' => array( 'item7', 'item8', 'item9')
));

How i can get this result above?

$filteredArray = array( 'item1', 'item2', 'item3', 'item4', 'item5', 'item6', 'item7', 'item8', 'item9');

I can do this with foreach, but have any other method more clean?

  • 写回答

4条回答 默认 最新

  • duanniu3385 2017-09-25 19:24
    关注

    You can reduce the itens column of your array, using array_merge as the callback.

    $filteredArray = array_reduce(array_column($all, 'itens'), 'array_merge', []);
    

    Or even better, eliminate the array_reduce by using the "splat" operator to unpack the column directly into array_merge.

    $result = array_merge(...array_column($all, 'itens'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助