您的位置:首页 > 编程语言 > PHP开发

[php]将树状文件目录数组转换成路径列表形式

2014-01-15 02:18 453 查看
function fileTreeToPathList($arrFileTree, $strPrevKey='')
{
	$arrPathList = array();
	foreach ($arrFileTree as $k => $node) {
		$strNowKey = $strPrevKey ? $strPrevKey.'/'.$k : $k;
		if (is_array($node)) {
			$arr = fileTreeToPathList($node, $strNowKey);
			$arrPathList = array_merge($arrPathList, $arr);
		} else {
			$arrPathList[$strNowKey] = $node;
		}
	}
	return $arrPathList;
}

class nodeclass{}

$arrFileTree = array(
	'a' => new nodeclass,
	'b' => new nodeclass,
	'c' => array(
		'd' => new nodeclass,
		'e' => new nodeclass,
		'f' => array(
			'g' => new nodeclass,
			'h' => new nodeclass,
		),
	)
);

$arrPathList = fileTreeToPathList($arrFileTree);
print_r($arrPathList);

// -- output --
Array
(
    [a] => nodeclass Object
        (
        )

    [b] => nodeclass Object
        (
        )

    [c/d] => nodeclass Object
        (
        )

    [c/e] => nodeclass Object
        (
        )

    [c/f/g] => nodeclass Object
        (
        )

    [c/f/h] => nodeclass Object
        (
        )
)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: