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  
)  

本书共88小节:

评论 (写第一个评论)