25. 合并数组

drupal8 龧龤龡龠龝龜龛龘龕龔龧龓龒龎龋龊龉龈龆龃龂 php 齾齼龧齻齷龈齴齱齰齬龧齩齨齦龔龈齣齠 drupal 齞齜齛龠龝齘齗齔龓龒齑齎龈齊齉齅齃鼿鼼齗鼸鼶鼴鼳鼱鼭鼫

龓龒 php 龎龋鼶鼪龛齉鼨鼥鼸齑齎鼣

1 鼣鼞鼝鼙 + 鼒龊龉鼐鼏鼎鼊龎龋鼉鼇龧鼅鼂黾龓龒

2 鼣黹龎鼣 array_merge 黬鼂黾龓龒

3 array_merge_recursive 黜默齠黖鼂黾龓龒

4 drupal 龠龝龧黒黎黋龓龒

通过“+”操作符号进行数组相加

黊黇黅黄鼸龎龋鼞鼝鼙+鼒龊龉鼐鼉鼇龈龂齗齔鼅鼂黾龓龒龈黀龂麽齩麹麸麷鼉麴龈麳麲龎麮麫麧麸麷龈麥鼅齼龧麤麡黬齼龧龈齉鼅麟麞麛龈麚齬麖麕齅齃麒麑龎麮麫麧麏鼴龈麋龧齅麇龂麆黖鼂黾鼅齼龧龎龋

齨黹龎麄齑麁麀鹿鹼鼣 http://php.net/manual/zh/function.array-merge.php

$a=["a"=>1,"b"=>2,"c"=>3,"a"];  
$b=["a"=>4,"d"=>5,"c"=>6,"b"];  
$c=array_merge($a,$b);  
那么$c将是["a"=>4,"b"=>2,"c"=>6,"a",d"=>5,"b"];  

array_merge_recursive 递归合并

齨黹龎龂齗齔黖鼂黾龧龓龒龈黅齊龛龎龋龧麟鹞鼂黾鼨鼳龈齅麇龂鹜麤麡龈黅龃鹚麤麡龧麟龓龒龋鹙鹗龎龋龈龎麮麫麧黅鹖鹗麫麧龈龆鼸鼝鹔龂黜默龧龈齩鼣鹓鹑龡鹼鹎鹌鹋鹇鹆鹄

$a = ["a" => ["a1", "a2"], "b" => ["b1", "b2"], "c" => 3, "a"];  
$b = ["a" => 4, "d" => 5, "c" => 6, "b", "b" => ["b1", "b3"]];  
$c = array_merge_recursive($a, $b);  
那么$c将是:  
Array  
(  
    [a] => Array  
        (  
            [0] => a1  
            [1] => a2  
            [2] => 4  
        )  
  
    [b] => Array  
        (  
            [0] => b1  
            [1] => b2  
            [2] => b1  
            [3] => b3  
        )  
  
    [c] => Array  
        (  
            [0] => 3  
            [1] => 6  
        )  
  
    [0] => a  
    [d] => 5  
    [1] => b  
)  
怎么理解“递归”呢?如下:  
$a = ["a" => ["b" => ["c" => 1]]];  
$b = ["a" => ["b" => ["c" => 2]]];  
$c = array_merge_recursive($a, $b);  
那么$c将是:  
Array  
(  
    [a] => Array  
        (  
            [b] => Array  
                (  
                    [c] => Array  
                        (  
                            [0] => 1  
                            [1] => 2  
                        )  
  
                )  
  
        )  
  
) 

drupal实现的合并

drupal龜龠龝齘齗齔齅鹃龧龓龒龈鹁齉鸽齊龛龓龒鹞鹜齗鸺龈鹑龡鸷齰
Drupal\Component\Utility\NestedArray::mergeDeep($arr1, $arr2, ...);
Drupal\Component\Utility\NestedArray:: mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE)
齉鸽鸊齗鼸龂鸊鸉鼸龧齅鹃鸅鸄龈鸀鼱鸊鸉鼸龈鹑龡齩鼨鼣鹓鹼鹎鹌鹋鹇鹆鹄

麋鹁php龧array_merge_recursive黹龎鷿鷽龈齴鷺龛鹆齃龈鼉鷽鷹龂鹞龂黜默龓龒龈鹜麴鷹龂鷷齉鷵鷲鷱龎龈齠鷭麟龛鷩鷦鼫
鷱龎鼣$preserve_integer_keys鷵鷲齘鷭龎麮麫麧鷢鷡龧鼊麞龈鷠鷞麞false,齨麖龎麮麫麧鹜鹚鼅鷛黬麤麡龈齣龂黖鷘鼂黾龈龒鹖鹗麫麧龈鹁array_merge鷔array_merge_recursive黹龎鼊麞齗鸺龈齩麹鷵鷲麞TRUE龈鷌鷋黅龎麮麫麧麆黖鷊麴齰龘龎麮麫麧鼫

public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) {  
    $result = array();  
    foreach ($arrays as $array) {  
      foreach ($array as $key => $value) {  
        if (is_integer($key) && !$preserve_integer_keys) {  
          $result[] = $value;  
        }  
        elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {  
          $result[$key] = self::mergeDeepArray(array($result[$key], $value), $preserve_integer_keys);  
        }  
        else {  
          $result[$key] = $value;  
        }  
      }  
    }  
    return $result;  
  }  

龘龎麮麫麧龧鷈鷇鷆龝齘鹁array_merge_recursive黹龎龧鹆齃鼣
drupal龠龝龂鼣鼉麴麸麷鷄鷃鷀龧麟鹞麞龎龋龈麥鼎鼊黜默龓龒龈鶾麥鶻鼪龛齗鼸鹜龂龎龋龈黅齬黬齼龧麟麤麡鼅齼龧麟鼫
齩鼣鹓鹼鹎鹌鹋鹇鹆鹄

$a = ["a" => 1,"a"];  
$b = ["a" => 4,"b"];  
$c = array_merge_recursive($a, $b);的结果是:  
Array  
(  
    [a] => Array  
        (  
            [0] => 1  
            [1] => 4  
        )  
  
    [0] => a  
    [1] => b  
)  
$c = NestedArray::mergeDeepArray([$a, $b],false);的结果是:  
Array  
(  
    [a] => 4  
    [0] => a  
    [1] => b  
)  
$c = NestedArray::mergeDeepArray([$a, $b],true);的结果是:  
Array  
(  
    [a] => 4  
    [0] => b  
)  
当值为数组时,行为如下:  
$a = ["a" => [1,2],"a"];  
$b = ["a" => [3,4],"b"];  
  
$c = array_merge_recursive($a, $b);时:  
Array  
(  
    [a] => Array  
        (  
            [0] => 1  
            [1] => 2  
            [2] => 3  
            [3] => 4  
        )  
  
    [0] => a  
    [1] => b  
)  
$c = NestedArray::mergeDeepArray([$a, $b],false);时和array_merge_recursive输出一致:  
Array  
(  
    [a] => Array  
        (  
            [0] => 1  
            [1] => 2  
            [2] => 3  
            [3] => 4  
        )  
  
    [0] => a  
    [1] => b  
)  
  
$c = NestedArray::mergeDeepArray([$a, $b],true);时  
Array  
(  
    [a] => Array  
        (  
            [0] => 3  
            [1] => 4  
        )  
  
    [0] => b  
)  

本书共65小节:

评论 (写第一个评论)