Blogger Information
Blog 11
fans 0
comment 0
visits 10134
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
关于PHP递归函数以及处理多维数组和目录文件实例
小杂鱼
Original
748 people have browsed it

递归函数

函数自身调用自身,但必须在调用自身前有条件判断,否则会无限调用下去。适用于树形结构

处理多维数组

  1. //将$arr多维数组重组成一个新数组
  2. $arr = [[1,2,3,['A', 'B', 'C',['F1','F2', 'F3']]],'a', [4,5,6,['D', 'E', 'F']],'b'];
  3. $newArr = [];
  4. function printArray($arr,&$newArr)
  5. {
  6. if(is_array($arr)){
  7. foreach ($arr as $i) {
  8. if(is_array($i)) {
  9. printArray($i, $newArr);
  10. }else {
  11. array_push($newArr,$i);
  12. }
  13. }
  14. }
  15. return $newArr;
  16. }
  17. print_r(printArray($arr,$newArr));
  18. //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => A [4] => B [5] => C [6] => F1 [7] => F2 [8] => F3 [9] => a [10] => 4 [11] => 5 [12] => 6 [13] => D [14] => E [15] => F [16] => b )

删除目录和文件

  1. function delete_dir_file($dir)
  2. {
  3. $res = false; //声明一个初始状态 默认情况下缓存未被删除
  4. if(is_dir($dir))
  5. {
  6. if($handle = opendir($dir))//成功打开目录流
  7. {
  8. while( ($file = readdir($handle)) != false)
  9. {
  10. if($file != '.' && $file != '..')
  11. {
  12. if(is_dir($dir.'\\'.$file)){
  13. delete_dir_file($dir.'\\'.$file);
  14. }else{
  15. unlink($dir.'\\'.$file);/unlink只能删除一个文件
  16. }
  17. }
  18. }
  19. }
  20. closedir($handle);//关闭目录句柄
  21. if(rmdir($dir)){//目录只有为空的情况下才能被直接删除
  22. $res = true;
  23. }
  24. }
  25. return $res;
  26. }
  27. $app_path = 'D:\PHP\phpstudy_pro\WWW\php.edu\0805';
  28. delete_dir_file($app_path ); //删除0805目录及其子目录和文件
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