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)鳂龢齷齥齡鹊齚鰼鰹黊鹃黝鹰鳬鳪鲷鶽鰶鳚鶙鵿鼨鰵鰳鼿鳈鹻

本书共63小节:

评论 (写第一个评论)