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

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
        (
        )
)

你可能感兴趣的:(PHP,目录树)