4 回答
TA贡献1853条经验 获得超9个赞
使用其他变量来保存累积的合并。
$originalArray = [1,2,3];
$newArray = [
'first' => ($tempArray = array_merge($originalArray, [4,5])),
'second' => ($tempArray = array_merge($tempArray, [6,7])),
'third' => ($tempArray = array_merge($tempArray, [8,9])),
...
];
TA贡献1806条经验 获得超8个赞
随循环合并,并使用上次合并更新原始数组。
<?php
$orig = [1,2,3];
$trans = [
'first' => [4,5],
'second' => [6,7],
];
foreach($trans as &$v) {
$orig = $v = array_merge($orig, $v);
}
unset($v);
var_export($trans);
输出:
array (
'first' =>
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
),
'second' =>
array (
0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
),
)
TA贡献1864条经验 获得超2个赞
以下是您要执行的操作:
$originalArray = [1,2,3];
$newArray = [
'first' => array_merge($originalArray, [4,5]),
'second' => array_merge($originalArray, [6,7]),
];
function val($newArray) { return $newArray; }
$originalArray = array_map( 'val' , $newArray);
print_r( $originalArray );
输出:
Array (
[first] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
[second] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 6 [4] => 7 )
)
TA贡献1805条经验 获得超10个赞
请尝试以下代码。它为您工作。
<?php
$originalArray = [1,2,3];
$newArray['first'] = array_merge($originalArray, [4,5]);
$newArray['second'] = array_merge($newArray['first'], [6,7]);
$newArray['third'] = array_merge($newArray['second'], [8,9]);
echo "<pre>";
print_r($newArray);
?>
=>输出
Array
(
[first] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
[second] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
)
[third] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
)
)
- 4 回答
- 0 关注
- 178 浏览
添加回答
举报