Blogger Information
Blog 15
fans 0
comment 0
visits 7781
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
请实例演绎你对回调函数与递归函数的理解?
ッ小眼睛っ
Original
382 people have browsed it
  1. <?php
  2. $arr=[
  3. ['id'=>1,'pid'=>0,'name'=>'体育'],
  4. ['id'=>2,'pid'=>0,'name'=>'娱乐'],
  5. ['id'=>3,'pid'=>0,'name'=>'科技'],
  6. ['id'=>4,'pid'=>0,'name'=>'新闻'],
  7. ['id'=>5,'pid'=>1,'name'=>'篮球'],
  8. ['id'=>6,'pid'=>1,'name'=>'足球'],
  9. ['id'=>7,'pid'=>1,'name'=>'乒乓球'],
  10. ['id'=>8,'pid'=>1,'name'=>'羽毛球'],
  11. ['id'=>9,'pid'=>2,'name'=>'八卦'],
  12. ['id'=>10,'pid'=>3,'name'=>'手机'],
  13. ['id'=>11,'pid'=>3,'name'=>'电脑'],
  14. ['id'=>12,'pid'=>4,'name'=>'国际新闻'],
  15. ['id'=>13,'pid'=>4,'name'=>'国内新闻'],
  16. ['id'=>14,'pid'=>12,'name'=>'欧洲新闻'],
  17. ['id'=>15,'pid'=>10,'name'=>'华为'],
  18. ['id'=>16,'pid'=>10,'name'=>'小米'],
  19. ['id'=>17,'pid'=>15,'name'=>'华为P50'],
  20. ['id'=>18,'pid'=>15,'name'=>'华为P40']
  21. ];
  22. //无限分级
  23. function wx(array $arr,int $pid,int $level){
  24. static $arr1;
  25. if(!empty($arr)){
  26. for($i=0;$i<count($arr);$i++){
  27. if($arr[$i]['pid'] == $pid){
  28. //其实想输出数组的可惜不会了
  29. echo str_repeat('&nbsp;&nbsp;&nbsp;',$level).$arr[$i]['name'].'<br>';
  30. wx($arr,$arr[$i]['id'],$level+1);
  31. }
  32. }
  33. }
  34. }
  35. //回调函数
  36. call_user_func_array('wx',[$arr,0,0]);
  37. //回调匿名函数
  38. $func=function (array $arr,int $pid,int $level){
  39. static $arr1;
  40. if(!empty($arr)){
  41. for($i=0;$i<count($arr);$i++){
  42. if($arr[$i]['pid'] == $pid){
  43. //其实想输出数组的可惜不会了
  44. echo str_repeat('&nbsp;&nbsp;&nbsp;',$level).$arr[$i]['name'].'<br>';
  45. wx($arr,$arr[$i]['id'],$level+1);
  46. }
  47. }
  48. }
  49. };
  50. call_user_func_array($func,[$arr,0,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