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  
)  

本书共78小节:

评论 (写第一个评论)