Blogger Information
Blog 46
fans 0
comment 0
visits 34458
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP部分数组函数实例演示
上草一方
Original
692 people have browsed it

1.array_change_key_case()
array_change_key_case(array $array, int $case = CASE_LOWER) 将 array 数组中的所有键名改为全小写或大写。本函数不改变数字索引。
array:需要操作的数组。
case:可以在这里用两个常量,CASE_UPPER 或 CASE_LOWER(默认值)。

  1. <?php
  2. $input_array = array("FirSt" => 1, "SecOnd" => 4);
  3. $stack = array_change_key_case($input_array, CASE_UPPER);
  4. printf('<pre>%s</pre>', print_r($stack, true));
  5. ?>

输出结果为:
Array
(
[FIRST] => 1
[SECOND] => 4
)

2.array_chunk(array $array, int $length, bool $preserve_keys = false): array
array:需要操作的数组
length:每个数组的单元数目
preserve_keys:设为 true,可以使 PHP 保留输入数组中原来的键名。如果你指定了 false,那每个结果数组将用从零开始的新数字索引。默认值是 false。

  1. $input_array = array('a', 'b', 'c', 'd', 'e','f');
  2. print_r(array_chunk($input_array, 2));
  3. echo '<br>';
  4. print_r(array_chunk($input_array, 2, true));

输出结果为:
Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [0] => c [1] => d ) [2] => Array ( [0] => e [1] => f ) )
Array ( [0] => Array ( [0] => a [1] => b ) [1] => Array ( [2] => c [3] => d ) [2] => Array ( [4] => e [5] => f ) )

3.array_column(array $array, int|string|null $column_key,int|string|null $index_key = null): array
array_column() 返回 array 中键名为 column_key 的一列值。 如果指定了可选参数 index_key,则使用输入数组中 index_key 列的值将作为返回数组中对应值的键。

  1. $records = array(
  2. array(
  3. 'id' => 2135,
  4. 'first_name' => 'John',
  5. 'last_name' => 'Doe',
  6. ),
  7. array(
  8. 'id' => 3245,
  9. 'first_name' => 'Sally',
  10. 'last_name' => 'Smith',
  11. ),
  12. array(
  13. 'id' => 5342,
  14. 'first_name' => 'Jane',
  15. 'last_name' => 'Jones',
  16. ),
  17. array(
  18. 'id' => 5623,
  19. 'first_name' => 'Peter',
  20. 'last_name' => 'Doe',
  21. )
  22. );

输出结果为:
Array ( [0] => John [1] => Sally [2] => Jane [3] => Peter )

4.array_count_values(array $array): array
array_count_values() 返回一个数组: 数组的键是 array 里单元的值; 数组的值是 array 单元的值出现的次数。

  1. $array = array(1, "hello", 1, "world", "hello");
  2. print_r(array_count_values($array));

输出结果为:
Array ( [1] => 2 [hello] => 2 [world] => 1 )

5.array_keys(array $array, mixed $search_value, bool $strict = false): array
array_keys() 返回 input 数组中的数字或者字符串的键名。
如果指定了可选参数 search_value,则只返回该值的键名。否则 input 数组中的所有键名都会被返回。

  1. $array = array(0 => 100, "color" => "red");
  2. print_r(array_keys($array));
  3. $array = array("blue", "red", "green", "blue", "blue");
  4. print_r(array_keys($array, "blue"));
  5. $array = array("color" => array("blue", "red", "green"),
  6. "size" => array("small", "medium", "large"));
  7. print_r(array_keys($array));

输出结果为:
Array ( [0] => 0 [1] => color ) Array ( [0] => 0 [1] => 3 [2] => 4 ) Array ( [0] => color [1] => size )

PHP数组函数可参考:https://www.php.net/manual/zh/ref.array.php

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post