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  
)  

本书共63小节:

评论 (写第一个评论)