Home > php教程 > php手册 > 简单递归遍历一个目录树

简单递归遍历一个目录树

WBOY
Release: 2016-06-06 19:38:12
Original
948 people have browsed it

无详细内容 无 ?php// 遍历一个多维数组,得到它们的目录关系$tree = array ( 'common.lua', 'prototype.lua', 'round' = array ( 'AI.lua', 'creature' = array ( 'boy.lua' ), 'round.lua', 'trump' = array ( 'firefan.lua', 'mirror.lua' ) ) );function

<?php
// 遍历一个多维数组,得到它们的目录关系
$tree = array
        (
            'common.lua',
            'prototype.lua',
            'round' => array
                (
                    'AI.lua',
                    'creature' => array
                        (
                            'boy.lua'
                        ),
                   	'round.lua',
                    'trump' => array
                        (
                            'firefan.lua',
                            'mirror.lua'
                        )
                )
        );

function iterate($data, $pre="")
{
	if (is_array($data)) 
	{				
		foreach($data as $key => $item)
		{									
			if(is_array($item)) {
				$pre .= $key."/";	
				iterate($item, $pre);				
				$pre = substr($pre, 0, strpos($pre, $key)); //回退到上层目录
			}			
			else
			{
				echo $pre.$item."\n";
			}				
		}
	}	
}		
iterate($tree);
?>
Copy after login
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template