数组合并和覆盖

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


$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]


现在,我所拥有的是,然后是然后是,这是完全有意义的,因为在两者和实例上合并,但没有被覆盖。$originalArray[1,2,3]$newArray['first'][1,2,3,4,5]$newArray['second'][1,2,3,6,7]$originalArrayfirstsecond$originalArray


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


慕沐林林
浏览 179回答 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])),    ...];

慕森卡

随循环合并,并使用上次合并更新原始数组。<?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; ),)

慕斯王

以下是您要执行的操作:$originalArray = [1,2,3];$newArray = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'first'&nbsp; => array_merge($originalArray, [4,5]),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'second' => array_merge($originalArray, [6,7]),];function val($newArray) { return $newArray; }$originalArray = array_map( 'val' , $newArray);print_r( $originalArray );输出:Array (&nbsp;[first] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )&nbsp;[second] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 6 [4] => 7 )&nbsp;)

holdtom

请尝试以下代码。它为您工作。<?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