我有以下数组:
Array (
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => Array
(
[0] => 1
[1] => 2
)
[1] => Array
(
[0] => 3
[1] => 4
)
)
)
我想要做的是将[1]的内部数组合并在一起,给我一个看起来像这样的新数组:
Array (
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[4] => 4
)
)
有关我如何能做到这一点的任何提示吗?谢谢
答案 0 :(得分:1)
<?php
$arr = array(
0 => array(
0 => 1,
1 => 2,
2 => 3
),
1 => array(
0 => array(
0 => 1,
1 => 2
),
1 => array(
0 => 3,
1 => 4
)
)
);
for ($i = 0; $i < count($arr); $i++) {
$fixedArray = array();
foreach ($arr[$i] as $key => $value) {
if (!is_array($value)) continue;
foreach ($value as $subKey => $subValue)
$fixedArray[] = $subValue;
}
if (count($fixedArray) > 0)
$arr[$i] = $fixedArray;
}
print_r($arr);
?>
输出:
Array
(
[0] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[1] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
有一种方法可以做到这一点,而不是说这是最好的方式,但它确实有效。
答案 1 :(得分:1)
这是我的:
<?php
$multiArray = [
0 => [
0 =>1,
1 => 2,
2 => 3
],
1 => [
0 => [
0 => 1,
1 => 2
],
1 => [
0 => 3,
1 => 4
]
]
];
$aux = [];
foreach($multiArray as $key => $arr){
foreach($arr as $k =>$a){
if (is_array($a) ){
$aux= $arr;
break;
}
}
}
list($item1, $item2) = $aux;
$new = array_merge($item1, $item2);
$multiArray[1] =$new;
print_r($multiArray);
?>