drrrdo0802 2019-03-14 09:08
浏览 69
已采纳

在数组块之后在php中合并多维数组

Here is my arrays after array chunk by 2

$a = array(0 => array("testA1", "testA2"), 1 =>array("testA3", "testA4"));

$b = array(0 => array("testB1", "testB2"), 1 =>array("testB3", "testB4"));

$c = array(0 => array("testC1", "testC2"), 1 =>array("testC3", "testC4"));

I want to merge these arrays in below format.

$result = array("testA1","testA2","testB1","testB2", "testC1", "testC2", 
"testA3", "testA4", "testB3", "testB4", "testC3", "testC4"
);

Is there any in-built function available or how to solve this?.

  • 写回答

2条回答 默认 最新

  • dongshi6969 2019-03-14 09:11
    关注

    Use array_merge twice with ... syntax:

    print_r(array_merge(...array_merge($a, $b, $c)));
    
    // 2nd version
    $parts = [];
    foreach($a as $k => $v) {
        $parts[$k] = array_merge($a[$k], $b[$k], $c[$k]);
    }
    print_r(array_merge(...$parts));
    
    // hard to understand version:
    $parts = array_map(null, $a, $b, $c);
    print_r(array_merge(...array_merge(...$parts)));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想编写一个期货跨期套利的程序
  • ¥15 UltraScale 系列 Bitslip 技术支持
  • ¥15 一个线程在sleep的时候set一个信号会起作用吗
  • ¥100 需求高精度PT100设计电路和算法
  • ¥15 单片机配网,继电器开关,广播
  • ¥60 Qcustomplot绘制实时动态曲线
  • ¥20 运用matlab画x-y图
  • ¥15 用idea运行项目,运行tomcat报错:断言失败
  • ¥15 Sqlserver查询链接服务器数据问题
  • ¥15 Bibtex4Word 引用中文文献