php常用内置函数-Array

PHP技术
426
0
0
2022-11-12
标签   PHP基础

array_chunk - 将一个数组分割成多个

  • array_chunk(array $array, int $length, bool $preserve_keys = false): array
  $arr = [1, 2, 3, 'h', 5=>'w'];
  //第三个参数设为true则保留原数组的键名
  print_r(array_chunk($arr, 3, true));
      //结果
      Array
      (
          [0] => Array
              (
                  [0] => 1
                  [1] => 2
                  [2] => 3
              )
          [1] => Array
              (
                  [3] => h
                  [5] => w
              )
      )

array_column - 返回数组中指定列的值

  • array_column(array $array, int|string|null $column_key, int|string|null $index_key = null): array
  $arr = [
              [
                  'id' => 1,
                  'age' => 13,
                  'name' => 'alex'
              ],
              [
                  'id' => 12,
                  'age' => 23,
                  'name' => 'bianca'
              ],
          ];
              //结果
              Array
              (
                  [0] => alex
                  [1] => bianca
              )

array_count_values - 统计数组中所有的值

  • array_count_values(array $array): array
  $arr = [1, 2, 3, 2, 1, 'age', 'name', 'age'];
  print_r(array_count_values($arr));
      //结果
      Array
      (
          [1] => 2
          [2] => 2
          [3] => 1
          [age] => 2
          [name] => 1
      )

array_diff - 计算数组的差集

  • array_diff(array $array, array …$arrays): array
  $arr1 = [1, 2, 3];
  $arr2 = [1, 3, 4];

  print_r(array_diff($arr1, $arr2));
      //结果
      Array
      (
          [1] => 2
      )

array_intersect - 计算数组的交集

  • array_intersect(array $array, array …$arrays): array
  $arr1 = ['a' => 3, 5, 7, 9];
  $arr2 = ['b' => 3, 5];
  print_r(array_intersect($arr1, $arr2));
      Array
      (
          [a] => 3
          [0] => 5
      )

array_filter - 使用回调函数过滤数组中的元素

  • array_filter(array $array, ?callable $callback = null, int $mode = 0): array
  $arr = [1, 2, 3, 4];
  print_r(array_filter($arr, function($v){return $v % 2 == 0;}));
      //结果
      Array
      (
          [1] => 2
          [3] => 4
      )

array_map - 给数组的每个元素执行回调函数

  • array_map(?callable $callback, array $array, array …$arrays): array
  $arr = [1, 2, 3, 4];
  print_r(array_map(function($v){return $v * 2;}, $arr));
  //结果
  Array
  (
      [0] => 2
      [1] => 4
      [2] => 6
      [3] => 8
  )

array_merge - 合并一个或者多个数组

  • array_merge(array …$arrays): array
  $arr1 = [1, 2, 3, 4];
  $arr2 = [1, 3,4, 'b'=>7];

  print_r(array_merge($arr1, $arr2));
      //结果
      Array
      (
          [0] => 1
          [1] => 2
          [2] => 3
          [3] => 4
          [4] => 1
          [5] => 3
          [6] => 4
          [b] => 7
      )

array_pop — 弹出数组最后一个单元(出栈)

  • array_pop(array &$array): mixed
  $arr = [1, 2, 3];

  echo array_pop($arr); //3

array_push — 将一个或多个单元压入数组的末尾(入栈)

  • array_push(array &$array, mixed $value1, mixed $… = ?): int
  $arr = [1, 2, 3];

  array_push($arr, 4);
  print_r($arr);
      Array
      (
          [0] => 1
          [1] => 2
          [2] => 3
          [3] => 4
      )

array_reduce — 用回调函数迭代地将数组简化为单一的值

  • array_reduce(array $array, callable $callback, mixed $initial = null): mixed
 $arr = [1, 2, 3];

 echo array_reduce($arr, function($a, $b){return $a + $b;}, 10); //16 
 //如果指定了第三个参数,则为回调函数的初始值

array_reverse — 返回单元顺序相反的数组

  • array_reverse(array $array, bool $preserve_keys = false): array
 $arr = [1, 2, 3];

 print_r(array_reverse($arr));
     Array
     (
         [0] => 3
         [1] => 2
         [2] => 1
     )

array_search — 在数组中搜索给定的值,如果成功则返回首个相应的键名

  • array_search(mixed $needle, array $haystack, bool $strict = false): int|string|false
  $arr = [1, 2, 3];

  echo array_search(2, $arr); //1

array_unique — 移除数组中重复的值

  • array_unique(array $array, int $flags = SORT_STRING): array
  $arr = [4, 2, 3, 3, 1];

  print_r(array_unique($arr));
      Array
      (
          [0] => 4
          [1] => 2
          [2] => 3
          [4] => 1
      )
  //此时会返回一个新数组,不会修改原数组

array_shift - 移除数组开头的元素

  • array_shift(array &$array): mixed
  $arr = [4, 2, 3];

  echo array_shift($arr); //4

array_unshift - 在数组开头插入一个或多个单元

  • array_unshift(array &$array, mixed …$values): int
  $arr = [4, 2, 3];

  array_unshift($arr, [1 ,2]);
  print_r($arr);
      Array
      (
          [0] => Array
              (
                  [0] => 1
                  [1] => 2
              )

          [1] => 4
          [2] => 2
          [3] => 3
      )

array_walk — 使用用户自定义函数对数组中的每个元素做回调处理

  • array_walk(array|object &$array, callable $callback, mixed $arg = null): bool
  $arr = [4, 2, 3];

  array_walk($arr, function($v, $k){echo $k . '===' . $v;}); //0===41===22===3

in_array — 检查数组中是否存在某个值

  • in_array(mixed $needle, array $haystack, bool $strict = false): bool
  $arr = [4, 2, 3];

  echo in_array(3, $arr); //1

compact — 建立一个数组,包括变量名和它们的值

  • compact(array|string $var_name, array|string …$var_names): array
  $str = 'php';
  $arr = [];
  print_r(compact('str', $arr));
      Array
      (
          [str] => php
      )

count — 统计数组、Countable 对象中所有元素的数量

  • count(Countable|array $value, int $mode = COUNT_NORMAL): int
  $arr = [1, 2, 3];
  echo count($arr); //3

krsort — 对数组按照键名逆向排序

  • krsort(array &$array, int $flags = SORT_REGULAR): bool
  $arr = ['d' => 1, 'c' => 2, 'e' => 3];
  krsort($arr);
  print_r($arr);
      Array
      (
          [e] => 3
          [d] => 1
          [c] => 2
      )

list — 把数组中的值赋给一组变量

  • list(mixed $var, mixed …$vars = ?): array
  $info = ['coffee', 'brown', 'caffeine'];
  list($drink, $color, $power) = $info;

range 根据范围创建数组,包含指定的元素

  • range(string|int|float $start, string|int|float $end, int|float $step = 1): array
  print_r(range(0, 3));
      Array
      (
          [0] => 0
          [1] => 1
          [2] => 2
          [3] => 3
      )

shuffle — 打乱数组

  • shuffle(array &$array): bool
  $arr = [1, 3, 4, 'b'=>7];

  shuffle($arr);
  print_r($arr);
      Array
      (
          [0] => 7
          [1] => 1
          [2] => 4
          [3] => 3
      )

sort - 对数组进行升序排序

  • sort(array &$array, int $flags = SORT_REGULAR): bool
  $arr = [5, 3, 4, 'b'=>7];

  sort($arr);
  print_r($arr);
      Array
      (
          [0] => 3
          [1] => 4
          [2] => 5
          [3] => 7
      )

array_splice 去掉数组中的某一部分并用其它值取代

  • 定义
  array_splice(
      array &$array,
      int $offset,
      ?int $length = null,
      mixed $replacement = []
  ): array 
  $arr = [1, 2, 3];

  array_splice($arr,1 , 0, 'insert');
  print_r($arr);
      Array
      (
          [0] => 1
          [1] => insert
          [2] => 2
          [3] => 3
      )
  //此函数可以向数组的指定位置插入元素

应用

  • 找出数组中最大值的下标
  $arr = [3, 5, 7, 9];
  echo array_search(max($arr), $arr); //3
  • pathinfo - 返回文件路径的信息
  //pathinfo(string $path, int $flags = PATHINFO_ALL): array|string
  $str = 'index.php';

  $file_attrs = pathinfo($str);
  print_r($file_attrs);
      Array
      (
          [dirname] => .
          [basename] => index.php
          [extension] => php
          [filename] => index
      )
  echo $file_attrs['extension']; //php