Blogger Information
Blog 64
fans 6
comment 2
visits 83031
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php数组函数
王娇
Original
727 people have browsed it

学习总结

  • 使用数组函数可以轻松取到数组中的键和值
  • 使用数组函数可以轻松查找数组中的键和值
  • 使用数组函数可以对数组的键或值进行排序
  • 使用数组函数中的回调函数可以对数组中的所有元素进行复杂的逻辑处理
  1. <?php
  2. //1.array_chunk(需要分割的数组,分割后每个数组的元素个数)数组分割函数
  3. $courseArr = [89,90,78,67];
  4. $courseGrade =array_chunk($courseArr,2) ;//返回多个数组
  5. printf('<pre>%s</pre>',print_r($courseGrade,true));
  6. //2.array_column(需要处理的数组,需要返回的键名)返回数组中指定一列的值,返回值是一个数组
  7. $grade = [
  8. ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 76],
  9. ['name' => 'Eric','chinese' => 98,'math' => 87,'english' => 74],
  10. ['name' => 'hugn','chinese' => 94,'math' => 77,'english' => 86],
  11. ['name' => 'peter','chinese' => 98,'math' => 57,'english' => 76],
  12. ['name' => 'Lucy','chinese' => 92,'math' => 87,'english' => 66],
  13. ];
  14. // printf('<pre>%s</pre>',print_r($grade,true));
  15. $chineseGrade = array_column($grade,'chinese');//返回所有同学的语文成绩
  16. printf('<pre>%s</pre>',print_r($chineseGrade,true));
  17. //3.array_key_exists(要查找的键名,要查找的数组)检查数组中是否存在指定的键名
  18. $StuGrade = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 76];
  19. if (array_key_exists('php',$StuGrade)):
  20. echo $StuGrade['name'],'php成绩为:',$StuGrade['php'];
  21. else:
  22. echo $StuGrade['name'],'不存在php成绩';
  23. endif;
  24. //4.array_diff()返回两个数组的差集
  25. $fruitArr1 = ['apple','pear','banana','Strawberry','peach'];
  26. $fruitArr2 = ['apple','pear','peach'];
  27. $fruitArr = array_diff($fruitArr1,$fruitArr2);//返回在第一个数组,但是不在第二个数组中的数据返回'banana','Strawberry'
  28. printf('<pre>%s</pre>',print_r($fruitArr,true));
  29. //5.array_diff_key()根据键名计算差集
  30. $stu1 = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 76];
  31. $stu2 = ['name' => 'Eric','chinese' => 98];
  32. $stu = array_diff_key($stu1,$stu2);//返回'math' => 87,'english' => 76
  33. printf('<pre>%s</pre>',print_r($stu,true));
  34. //6.array_fill_keys()使用指定的键和值来填充数组
  35. $keyStr = ['chinese','math','english','php','mysql','html'];
  36. $valueStr = 0;
  37. $stuGrade = array_fill_keys($keyStr,$valueStr);//用指定的值填充指定键名,如果值也是一个数组,则返回一个二维数组
  38. printf('<pre>%s</pre>',print_r($stuGrade,true));
  39. //7.array_intersect 返回两个数组的交集
  40. $fruitArr1 = ['apple','pear','banana','Strawberry','peach'];
  41. $fruitArr2 = ['apple','pear','peach'];
  42. $fruitArr = array_intersect($fruitArr1,$fruitArr2);//返回在第一个数组,但是不在第二个数组中的数据返回'apple','pear','peach'
  43. printf('<pre>%s</pre>',print_r($fruitArr,true));
  44. //8.array_intersect_assoc() 返回两个数组的交集,键名也比较
  45. $stu1 = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 98];
  46. $stu2 = ['name' => 'Eric','chinese' => 98];
  47. $stu = array_intersect_assoc($stu1,$stu2);//返回'chinese' => 98键和值都相等才可以
  48. printf('<pre>%s</pre>',print_r($stu,true));
  49. //9.array_merge 合并多个数组
  50. $stu1 = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 98];
  51. $stu2 = ['name' => 'Eric','chinese' => 87];
  52. $stu = array_merge($stu1,$stu2);//合并数组,如果键名相同,则该键名对应的是最后一个数组中的值
  53. printf('<pre>%s</pre>',print_r($stu,true));
  54. //10.array_rand()随机返回数组中的一个或多个键名
  55. $fruitArr1 = ['apple','pear','banana','Strawberry','peach'];
  56. $fruitArr = array_rand($fruitArr1,3);//随机返回数组中的3个键级成一个数组
  57. //根据这个随机键名可以访问到该数组的随机值
  58. echo $fruitArr1[$fruitArr[0]],'<br>',$fruitArr1[$fruitArr[1]],'<br>',$fruitArr1[$fruitArr[2]],'<br>';
  59. //11.array_slice()函数 在数组中取出指定位置的数组元素
  60. $stu1 = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 76];
  61. $stu = array_slice($stu1,1,3);//返回数组中从第二个开始的3个数组元素'chinese' => 98,'math' => 87,'english' => 76
  62. printf('<pre>%s</pre>',print_r($stu,true));
  63. //12.array_splice()函数 删除并替换指定数组元素的值
  64. $stu1 = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 76];
  65. $grade = ['chinese' => 88,'math' => 89,'english' => 78];
  66. $stu = array_splice($stu1,1,3,$grade);//返回被删除的数组元素,替换数组直接替换原数组的数据,不保留键名
  67. printf('<pre>%s</pre>',print_r($stu1,true));
  68. //数组元素的回调函数
  69. //1.array_filter()过虑数组中的键或键和值
  70. $stu1 = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 76];
  71. $chiGrade = array_filter($stu1,function($k){//使用回调只返回语文成绩
  72. return $k=='chinese';
  73. },ARRAY_FILTER_USE_KEY);
  74. printf('<pre>%s</pre>',print_r($chiGrade,true));
  75. $stu1 = ['name' => 'angle','chinese' => 68,'math' => 87,'english' => 76];
  76. $chiGrade = array_filter($stu1,function($v,$k){//使用回调返回语文成绩或者成绩大于80的科目
  77. return $k=='chinese' || $v>80;
  78. },ARRAY_FILTER_USE_BOTH);
  79. printf('<pre>%s</pre>',print_r($chiGrade,true));
  80. //1.array_reduce()用回调函数迭代的将数组元素简化为单一的值
  81. $arr = [1,2,3,4,5];
  82. $sum = array_reduce($arr,function($sum,$item){//第1次调用$sum=100,$item=1 返回101
  83. $sum+=$item; //第2次调用$sum=101,$item=2 返回103
  84. return $sum; //第3次调用$sum=103,$item=3 返回106
  85. },100); //第4次调用$sum=106,$item=4 返回110
  86. echo $sum,'<br>'; //第5次调用$sum=110,$item=5 返回115
  87. //1.array_map()//用回调函数处理每个数组中的每个元素
  88. $arr1 = [1,2,3,4,5];
  89. $arr2 = [2,4,6,8,10];
  90. $arr3 = [1,3,5,7,9];
  91. $arr = array_map(function($v1,$v2,$v3){
  92. return $v1+$v2+$v3;
  93. },$arr1,$arr2,$arr3);
  94. printf('<pre>%s</pre>',print_r($arr,true));
  95. //1.array_walk()//可以对数组中的所有值和键做回调
  96. $stu1 = ['name' => 'angle','chinese' => 98,'math' => 87,'english' => 76];
  97. array_walk($stu1,function($v,$k){
  98. echo $k,'=>',$v,'<br>';
  99. })
  100. ?>
  • 运行效果图
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:关于回调函数, 知道他们的用途, 如果想得到一个新数组,用array_map(), 如果只是对原数组进行格式化,用array_walk(), 如果想过滤掉数组中的某些数据, 用array_filter(), 但是实际中, 却同一个功能有多个数组可用的
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