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  
)  

本书共69小节:

评论 (写第一个评论)