38. 数组操作

龵drupal龣龠龝龙龕龔龒龎龊龈龆龅龃龀php齻龆齸齷齵齲龔齮齪齧齦齢齞齚齙齕齓齮齏齍drupal齊齆齅齃齁龙龕龒龎龔鼿齮龵鼼鼺鼹鼷鼵鼳鼱鼭鼩鼨龙龕鼦鼢鼞鼝鼛龔鼗鼕齮鼼鼩鼑鼍鼊鼈鼅齃鼃鼿鼁龔龙龕龒龎齮齧齦龀黾

龃黼黸黶龙龕黲黮黾Drupal\Component\Utility\ NestedArray

黆黄黃黀龙龕麼麺黾Drupal\Component\Utilit\ DiffArray

麴龙龕麱麭麩麨黾Drupal\Component\Utility\SortArray

鼿麣麡麠龝黾\core\lib\Drupal\Component\Utility龣齮麗麖麕麓龔龀龵麑齁麣麡麐龣龔鼿麌龀web麉麇麄齓龔麃齓麂麀鼿齮麉麇鹿鹾鼨鹽鹼齓龝鼈鼅鹸鹵龣齮鹳鹲龅龃鹮鹪鹦

鹣鹢麕麕龃黼黸黶龙龕黲黮鼿黾Drupal\Component\Utility\ NestedArray

鼿鹡鹞鹛龀鼨鹘鹖鹡鹞齵齲齮鹡鹔齕齓齮鹢鹒鹑鼭齁龊龈黾

龵鹐麴鹏龣鼨$this->get(鹅a.b.c鹁)鹒鸽鸹$this->data[鹅a鹁][鹅b鹁][鹅c鹁]鸳鸲麃鸮鸬龵鸨鸤鼺鸠龣鸞齓鸛齅鸚鸗鸓齮龠鸑鸎鸍鸋龀鸊a.b.c麑鸈龔鸆鸃鸂鷿龀鹾鷽龔齮鷼鸞龀麕龙龕鹾鼨龀龃鷻黸黶龔齮麑鸞龀NestedArray::getValue鹡鹞龔龎齓黾

鷳麣麡鸊鷰黾

  public static function &getValue(array &$array, array $parents, &$key_exists = NULL) {
    $ref = &$array;
    foreach ($parents as $parent) {
      if (is_array($ref) && array_key_exists($parent, $ref)) {
        $ref = &$ref[$parent];
      }
      else {
        $key_exists = FALSE;
        $null = NULL;
        return $null;
      }
    }
    $key_exists = TRUE;
    return $ref;
  }

鷊鼭齁鷇龙龀鷆鷂鷀龔龙龕

鹾鼨鼑鼵麣齵鸛龔a.b.c鼨鸋鼞鼗鶾鶺鶷鼗鼞龙龕龎鼞鸚鹡鹞龔鷊鶳齁鷇龙鸞鹾鼨麖鸛齧龔麗齅鹦鸎鸍鸊鶱鸚鷇龙鼞鶭龙龕鼑鶪鶨鶤齁鶠龙龕鹦

鷊鶜齁鷇龙鶚鸲鸍鶗鶖鸬鸚鹡鹞鶓麗鶒鶏龵鹮鼑鶪鶨NULL齮齪鶍齦鸳鸲鶋鶇龀麗鶒鶏龵鶄龀鶏龵齪麗鼞NULL鶖鸬麑鸞龀鷊鶜齁鷇龙龔龎齓齮鶓麗鶏龵鹮齧鶃齞鶂鸤鼞true齮齧鼨鶁齓鹡鵾鵼黆齮鶒龊鷆龠鸎鸚鵹鵶鹾鶒鵼黆鹦鸊鶱鷊鶳齁鷇龙鼞鶭龙龕齮鸚鷇龙鷼鶃齞鶂鸤鼞true

龆get鸞龆set齮麴鵴龔鹡鹞鼞黾

  public static function setValue(array &$array, array $parents, $value, $force = FALSE) {
    $ref = &$array;
    foreach ($parents as $parent) {
      // PHP auto-creates container arrays and NULL entries without error if $ref
      // is NULL, but throws an error if $ref is set, but not an array.
      if ($force && isset($ref) && !is_array($ref)) {
        $ref = [];
      }
      $ref = &$ref[$parent];
    }
    $ref = $value;
  }

鼵鵤齁鷇龙鵣get鹡鹞鸍鵡鵠鵜齮鷊鶜齁鼞鷆鶂鸤龔麗齮鷊鵙齁鷇龙$force龀鶚鸲鸍鶗鶖鸬

鸊鼵麣麴a.b.c鵕麗齮鵒麴$this->data[鹅a鹁][鹅b鹁][鹅c鹁]鵕麗齮齍鹮鸊鶱$this->data[鹅a鹁]龔麗龀鼭齁鵐龙龕麗齮鵍鶒龀龃黼龙龕齮鵌鸲鸳鸲黲黮鸬龀鵋鵈鵆鵄鶖鶄龀鵂鴾鶖鸬麑鸞龀$force龔龎齓齮鼞true鹮鴼龀鵆鵄齮鶠鹒龔$this->data[鹅a鹁]麗鼑鴸鴷齮鴴齓鴲龔鵆鵄鴮龔麗齮鸊鶱鼞false鵌鸲php鼑鴪鴨鼭齁鴾鴧齮鴾鴧鼿鼁鸊鷰鼹鴤鴠鴟鴛黾

$yunke="love";

$ref=&$yunke["ok"];

$ref="you";

鸚鹡鹞$force鴙鴖鼞false齮麑鸈鸊鶱齞鵕麗龙龕鶒鶺鼦鴔鴒鷆龈齮鵌鸲鶃鴪鴨鼨鴐鴾鴧齮鶂鸤鼞true鼑鴼龀鵆鵄齮鶒鶃鴪鴨鴾鴧鹦

鸹鴍鼭齁龙龕鸑黾

  public static function unsetValue(array &$array, array $parents, &$key_existed = NULL) {
    $unset_key = array_pop($parents);
    $ref = &self::getValue($array, $parents, $key_existed);
    if ($key_existed && is_array($ref) && array_key_exists($unset_key, $ref)) {
      $key_existed = TRUE;
      unset($ref[$unset_key]);
    }
    else {
      $key_existed = FALSE;
    }
  }

鸎鸍鸊鶱鷊鶳齁鷇龙鼞鶭龙龕齮鵌鸲鶠龙龕鶒鴊鴈鴄齮鶤齁鹡鹞齕齓鸛齅鼭齁鴃鳿鳻鳹齮鸊鷰黾

$a=["x"=>"a","b"];

$b=&$a["x"];

unset($b);

麑鸈龀齻龆齓龔齮鳸龀鸹鴍鶁齓鷽鳷$b齮鷽鳷$a鼢鶒鴊鴈鴄齮齪鸊鶱麑鸈黾

$a=["x"=>"a","b"];

$b=&$a;

unset($b["x"]);

鵌鸲鸞龆鳵齅齮鶠鷽鳷$a鼑鳳鷽

鳱鳭龙龕龣鹮鳬鶏龵鹐齁鸑黾

  public static function keyExists(array $array, array $parents) {
    // Although this function is similar to PHP's array_key_exists(), its
    // arguments should be consistent with getValue().
    $key_exists = NULL;
    self::getValue($array, $parents, $key_exists);
    return $key_exists;
  }

麑齁鹡鹞鳧鳥鳣鳢齮鳟鹛鳛齓齅getValue鹡鹞齮鶒龃鳘

龙龕鼦鼢黾

  public static function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) {
    $result = [];
    foreach ($arrays as $array) {
      foreach ($array as $key => $value) {
        // Renumber integer keys as array_merge_recursive() does unless
        // $preserve_integer_keys is set to TRUE. Note that PHP automatically
        // converts array keys that are integer strings (e.g., '1') to integers.
        if (is_integer($key) && !$preserve_integer_keys) {
          $result[] = $value;
        }
        // Recurse when both values are arrays.
        elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
          $result[$key] = self::mergeDeepArray([$result[$key], $value], $preserve_integer_keys);
        }
        // Otherwise, use the latter value, overriding any previous value.
        else {
          $result[$key] = $value;
        }
      }
    }
    return $result;
  }

鳔龙龕鼦鼢鹾鳒鼼鼺鼹鳑鳏龔鼝鼛齮齍黲鳎鳌鳊鼱鹒鳈鼑龃齁鷆鼦鼢龔龙龕龕鳄鼭齁龙龕龎鼞鷊鼭齁鷇龙齮齧鵣php龔array_merge_recursive鳂龙鼿鳀齮齪齢龆鵹鵶齮鵠鳀鸋龀麌龀黆黄鼦鼢齮鶒鵜鸋龀鹾鼨鲼鲸鷇龙齮鲷麴麗龆鳱鳭鹦

鷇龙黾$preserve_integer_keys鲼鲸齅麴龙鲴鲲鶁麀鲮龔麭鼞齮鴙鴖鼞false,齍鹮龙鲴鲲鶁鶒鶃鼵鲬鴮鵆鵄齮鵍龀鹛鲨鲤鲠齮鼢鲞鴲鲲鶁齮鵣array_merge鲛array_merge_recursive鳂龙麭鼞鼭鸈齮鸊鶱鲼鲸鼞TRUE齮鵌鸲鼑龙鲴鲲鶁鳟鹛鲗鵜龝鵐龙鲴鲲鶁鹦

鵐龙鲴鲲鶁龔黲黮鲓鸮齅鵣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
)

黆黄鼱鱴龙龕黾

  public static function filter(array $array, callable $callable = NULL) {
    $array = is_callable($callable) ? array_filter($array, $callable) : array_filter($array);
    foreach ($array as &$element) {
      if (is_array($element)) {
        $element = static::filter($element, $callable);
      }
    }

    return $array;
  }

Php龔龙龕鼱鱴鳂龙鶒龀黆黄龔齮鸊鶱齻龆齵齲 callback 鳂龙齮鼑鱰鱬 array 龣鴟龆鲗麗鼞 FALSE 龔鱩鹵齮鲗麗齕齓鱥鱤鳧鳥齮鶒龀鱠鱜鳧鳥鹦

黆黄黃黀龙龕麼麺黾Drupal\Component\Utilit\DiffArray

Php齵齲齅鼭鱛黃黀龙龕麼麺龔鳂龙齮麑齁鼿DiffArray龀array_diff_assoc()龔鼭齁鷽鼃齮array_diff_assoc()鱚鹡麣鱘鹾鼨鹑 http://www.php.net/manual/zh/function.array-diff-assoc.php 齮齧龀齆鸑鲒鳧鳥龙龕麼麺齮鷼鸞龀鸑鲒鵣鸑麗鱔鵠鲗鱒鴖鼞鵠鲗鵍鱐鱬齮齪麴龝龃黼龙龕鳎鳎齵齲鷊鼭黼龔鳧鳥齮鵍麑齁鼿鼑麱麭黆黄鳧鳥鹦

麴龙龕麱麭麩麨黾Drupal\Component\Utility\SortArray

麑齁鼿齓龝麴龙龕鱏鱍麩麨齮鵣drupal龠鱋龀鳧鳥鱇鱄龔齮龵drupal鼺鸠龣鱁麄龊鷆麴鼹鱀鰼鰹鲞鲬鰸鼛麱麭麩麨齮麑齁鼿鸞龀鼞齍鵍鶂黃齮麃鸮鳧鳥鳣鳢齮鶒鰵鳘齮麗麖鸎鸍龔龀齻龆鲼鲸鰹鲞齮鵌鸲鴙鴖鼞0齮麴龝鰸鼛龔鳧鳥龀齕齓php龔strnatcasecmp($a_title, $b_title)鳂龙齮齕齓鹅齊鰳鰰麨鹁黀鹞鳧鳥鲴鶺鰮鳔鶒鵹鼗鰭鰩鼳鳈鹦

本书共83小节:

评论 (写第一个评论)