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

时间:2019-03-14 09:08:13

标签: php arrays

这是我的数组在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"));

我想以以下格式合并这些数组。

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

是否有任何内置功能可用或如何解决?

2 个答案:

答案 0 :(得分:3)

使用array_merge语法两次使用...

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 :(得分:0)

Y用作Z

请参见this