博客列表 >array_change_key_case,array_column,array_chunk,array_map,array_filter,array_reduce

array_change_key_case,array_column,array_chunk,array_map,array_filter,array_reduce

秋闲独醉
秋闲独醉原创
2022年08月12日 10:39:48296浏览
  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 ) )
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议