PHP:递归遍历结果保存到多维数组

WBOY
Release: 2016-06-06 20:46:17
Original
1495 people have browsed it

请问,如何使用php实现递归遍历给定路径(比如:$path = c:)下的所有子目录,运行后能将目录名称保存为如下格式

<code>array
  'a' => 
    array
      0 => string 'a1' (length=2)
      1 => string 'a2' (length=2)
      2 => string 'a3' (length=2)
  'b' => 
    array
      'b1' => 
        array
          0 => string 'b11' (length=3)
          1 => string 'b12' (length=3)
      0 => string 'b2' (length=2)
  'c' => 
    array
      0 => string 'c1' (length=2)
      1 => string 'c2' (length=2)
      'c3' => 
        array
          0 => string 'c31' (length=3)
          1 => string 'c32' (length=3)
</code>
Copy after login
Copy after login

我目前实现的,只能将结果保存到一维数组中,代码如下:

<code>function get_subdir_path($path,&$dir_array,$sub_dir = TRUE)
{
    global $files_array;
    if (is_dir($path))
    {
        if($handle = opendir($path))
        {
            while (($file = readdir($handle))!=false)
            {
                if ($file !="." && $file != "..")
                {
                    $temp_path = $path ."/".$file;
                    if (is_dir($temp_path)){
                        array_push($dir_array,$temp_path);
                        if ($sub_dir==TRUE) {
                            get_subdir_path($temp_path,$dir_array);
                        }
                    }
                }
            }
        }
    }
}
</code>
Copy after login
Copy after login

类似如下图所示:
PHP:递归遍历结果保存到多维数组

回复内容:

请问,如何使用php实现递归遍历给定路径(比如:$path = c:)下的所有子目录,运行后能将目录名称保存为如下格式

<code>array
  'a' => 
    array
      0 => string 'a1' (length=2)
      1 => string 'a2' (length=2)
      2 => string 'a3' (length=2)
  'b' => 
    array
      'b1' => 
        array
          0 => string 'b11' (length=3)
          1 => string 'b12' (length=3)
      0 => string 'b2' (length=2)
  'c' => 
    array
      0 => string 'c1' (length=2)
      1 => string 'c2' (length=2)
      'c3' => 
        array
          0 => string 'c31' (length=3)
          1 => string 'c32' (length=3)
</code>
Copy after login
Copy after login

我目前实现的,只能将结果保存到一维数组中,代码如下:

<code>function get_subdir_path($path,&$dir_array,$sub_dir = TRUE)
{
    global $files_array;
    if (is_dir($path))
    {
        if($handle = opendir($path))
        {
            while (($file = readdir($handle))!=false)
            {
                if ($file !="." && $file != "..")
                {
                    $temp_path = $path ."/".$file;
                    if (is_dir($temp_path)){
                        array_push($dir_array,$temp_path);
                        if ($sub_dir==TRUE) {
                            get_subdir_path($temp_path,$dir_array);
                        }
                    }
                }
            }
        }
    }
}
</code>
Copy after login
Copy after login

类似如下图所示:
PHP:递归遍历结果保存到多维数组

今天刚刚说完一个人又来了一个范例。
少年,请问你看到最后面的一串花括号你头疼么?整整有8级以上的缩进啊,这还能让人看么!果断要差评好不好!

<code>function scan_rescursive($directory) {
    $res = array();
    foreach(glob("$directory/*") as $item) {
        if(is_dir($item)) {
            $folder = end(explode('/', $item));
            $res[$folder] = scan_rescursive($item);
            continue;
        }
        $res[] = basename($item);
    }
    return $res;
}
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template