Blogger Information
Blog 37
fans 0
comment 0
visits 14333
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
array_change_key_case,array_column,array_chunk,array_map,array_filter,array_reduce
秋闲独醉
Original
297 people have browsed it
  1. <?php
  2. //array_change_key_case(array $array,int $case):array;将数组的所有键名转为小写或者大写
  3. //$case :CASE_LOWER 小写;CASE_UPPER()大写
  4. $arr =['Id'=>001,'Name'=>'小红','Age'=>18,'Sex'=>'0'];
  5. print_r($arr); //Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 )
  6. print_r(array_change_key_case($arr,CASE_LOWER)); //Array ( [id] => 1 [name] => 小红 [age] => 18 [sex] => 0 )
  7. print_r(array_change_key_case($arr,CASE_UPPER)); //Array ( [ID] => 1 [NAME] => 小红 [AGE] => 18 [SEX] => 0 )
  8. //array_chunk(array $array,int $length,bool $preserver_keys = false):array 将一个数组分割成多个
  9. //$preserver_keys 设为true,可以使PHP保留原数组的键名,设为false,则重新数字索引.
  10. $arr = range(0,50,3);
  11. print_r($arr); //Array ( [0] => 0 [1] => 3 [2] => 6 [3] => 9 [4] => 12 [5] => 15 [6] => 18 [7] => 21 [8] => 24 [9] => 27 [10] => 30 [11] => 33 [12] => 36 [13] => 39 [14] => 42 [15] => 45 [16] => 48 )
  12. print_r(array_chunk($arr,5)); // Array ( [0] => Array ( [0] => 0 [1] => 3 [2] => 6 [3] => 9 [4] => 12 ) [1] => Array ( [0] => 15 [1] => 18 [2] => 21 [3] => 24 [4] => 27 ) [2] => Array ( [0] => 30 [1] => 33 [2] => 36 [3] => 39 [4] => 42 ) [3] => Array ( [0] => 45 [1] => 48 ) )
  13. //array_column(array $array,int|string|null $column_key,int|string|null $index_key=null):array
  14. //返回输入数张中指定列的值;
  15. $arr =[
  16. ['Id'=>001,'Name'=>'小红','Age'=>18,'Sex'=>'0'],
  17. ['Id'=>002,'Name'=>'小明','Age'=>28,'Sex'=>'1'],
  18. ['Id'=>003,'Name'=>'小绿','Age'=>38,'Sex'=>'0']
  19. ];
  20. print_r($arr); // Array ( [0] => Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 ) [1] => Array ( [Id] => 2 [Name] => 小明 [Age] => 28 [Sex] => 1 ) [2] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 38 [Sex] => 0 ) )
  21. print_r(array_column($arr,null,'Id')); //Array ( [1] => Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 ) [2] => Array ( [Id] => 2 [Name] => 小明 [Age] => 28 [Sex] => 1 ) [3] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 38 [Sex] => 0 ) )
  22. print_r(array_column($arr,'Name','Id')); //Array ( [1] => 小红 [2] => 小明 [3] => 小绿 )
  23. //array_reduce(raay $array,callable $callback,mixed $initial = null):mixed
  24. //用回调函数迭代地将数组简化为单一的值
  25. $arr =[
  26. ['Id'=>001,'Name'=>'小红','Age'=>18,'Sex'=>'0'],
  27. ['Id'=>002,'Name'=>'小明','Age'=>28,'Sex'=>'1'],
  28. ['Id'=>003,'Name'=>'小绿','Age'=>38,'Sex'=>'0']
  29. ];
  30. function fun1($carry,$item){
  31. $carry += $item['Age'];
  32. return $carry;
  33. }
  34. $result = array_reduce($arr,'fun1');
  35. print_r($result); //84
  36. //array_map(callbale $callback,array $array,array...$arrays):array
  37. //为数组的每个元素应用回调函数
  38. function fun2($item){
  39. //每个人都加上2岁
  40. $item['Age'] += 2;
  41. return $item;
  42. }
  43. $result = array_map('fun2',$arr);
  44. print_r($result); //Array ( [0] => Array ( [Id] => 1 [Name] => 小红 [Age] => 20 [Sex] => 0 ) [1] => Array ( [Id] => 2 [Name] => 小明 [Age] => 30 [Sex] => 1 ) [2] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 40 [Sex] => 0 ) )
  45. //array_filter(array $array,?callabel $callback = null, int $mode = 0):array
  46. //使用回调函数过滤数组的元素
  47. function fun3($item){
  48. //过滤掉小明
  49. if($item['Id'] != 002){
  50. return $item;
  51. }
  52. }
  53. $result = array_filter($arr,'fun3');
  54. print_r($result); //Array ( [0] => Array ( [Id] => 1 [Name] => 小红 [Age] => 18 [Sex] => 0 ) [2] => Array ( [Id] => 3 [Name] => 小绿 [Age] => 38 [Sex] => 0 ) )
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