数组合并和覆盖

我想知道是否可以合并两个数组,然后覆盖原始数组的属性。


$originalArray = [1,2,3];

$newArray = [

         'first'  => array_merge($originalArray, [4,5]),

         'second' => array_merge($originalArray, [6,7]),

];

所以我想要实现的是$originalArray,[1,2,3]然后$newArray['first']是[1,2,3,4,5],然后$newArray['second']是[1,2,3,4,5,6,7]


现在,我所拥有的是$originalArrayis [1,2,3], then $newArray['first']is[1,2,3,4,5]和 then $newArray['second']is [1,2,3,6,7],这是非常有意义的,因为$originalArray它被合并到两个实例上first,但没有被覆盖。second$originalArray


我想知道这是否可能?我们可以中间覆盖$originalArraywithin的值$newArray吗?


慕标5832272
浏览 150回答 4
4回答

胡说叔叔

使用附加变量来保存累积的合并。$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])),    ...];

慕田峪9158850

这是您想要执行的操作:$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 ) )

慕的地10843

使用循环进行合并,并使用最后一次合并更新原始数组。<?php$orig&nbsp; = [1,2,3];$trans = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'first'&nbsp; => [4,5],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'second' => [6,7],];foreach($trans as &$v) {&nbsp; &nbsp; $orig = $v = array_merge($orig, $v);}unset($v);var_export($trans);输出:array (&nbsp; 'first' =>&nbsp;&nbsp; array (&nbsp; &nbsp; 0 => 1,&nbsp; &nbsp; 1 => 2,&nbsp; &nbsp; 2 => 3,&nbsp; &nbsp; 3 => 4,&nbsp; &nbsp; 4 => 5,&nbsp; ),&nbsp; 'second' =>&nbsp;&nbsp; array (&nbsp; &nbsp; 0 => 1,&nbsp; &nbsp; 1 => 2,&nbsp; &nbsp; 2 => 3,&nbsp; &nbsp; 3 => 4,&nbsp; &nbsp; 4 => 5,&nbsp; &nbsp; 5 => 6,&nbsp; &nbsp; 6 => 7,&nbsp; ),)

三国纷争

试试下面的代码。它为你工作。<?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(&nbsp; &nbsp; [first] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => 5&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [second] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [5] => 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [6] => 7&nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; [third] => Array&nbsp; &nbsp; &nbsp; &nbsp; (&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [0] => 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [1] => 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [2] => 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [3] => 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [4] => 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [5] => 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [6] => 7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [7] => 8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [8] => 9&nbsp; &nbsp; &nbsp; &nbsp; ))
打开App,查看更多内容
随时随地看视频慕课网APP