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)鳑龟齴齙齖鹖齍鱊鱆黆鹑黛鹹鴃鴂鳅鷍鱅鳮鶩鶈鼝鱃鱀鼶鳘麁

本书共71小节:

评论 (写第一个评论)