我有4个数组,我需要通过键'0'组合:
Array
(
[0] => 1
[1] => Array
(
[0] => A
[1] => B
[2] => C
)
)
Array
(
[0] => 2
[1] => Array
(
[0] => D
[1] => E
[2] => F
)
)
Array
(
[0] => 1
[1] => Array
(
[0] => G
[1] => H
[2] => I
)
)
Array
(
[0] => 2
[1] => Array
(
[0] => J
[1] => K
[2] => L
)
)
我需要得到的是组合数组,应该是这样的:
Array
(
[0] => 1
[1] => Array
(
[0] => A
[1] => B
[2] => C
[3] => G
[4] => H
[5] => I
)
)
Array
(
[0] => 2
[1] => Array
(
[0] => D
[1] => E
[2] => F
[3] => J
[4] => K
[5] => L
)
)
我尝试过使用array_merge_recursive但没有成功。 也在这里搜索,抱歉,如果重复 在此先感谢您的帮助!
答案 0 :(得分:0)
试试这个:
$init = array(
array(1, array('A', 'B', 'C')),
array(2, array('D', 'E', 'F')),
array(1, array('G', 'H', 'I'))
);
$results = array();
foreach ( $init as $array ){
$found = false;
foreach ( $results as &$result ){
if ( $array[0] === $result[0] ){
$result[1] = array_merge($result[1], $array[1]);
$found = true;
}
}
if ( !$found ){
$results[] = $array;
}
}