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

简单递归遍历一个目录树

WBOY
WBOYOriginal
2016-06-06 19:38:12950browse

无详细内容 无 ?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);
?>
Statement:
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
Previous article:PHP全组合算法Next article:演示session和cookie