你好我只是在寻找一些帮助,因为我已经卡住了
我有两个字符串:
C:\Users\Bob\My Documents
/Users/Bob/Documents
通过
preg_split('/(?<=[\/\\\])(?![\/\\\])/', $string)
返回
Array
(
[0] => C:\
[1] => Users\
[2] => Bob\
[3] => My Documents
)
Array
(
[0] => /
[1] => Users/
[2] => Bob/
[3] => Documents
)
我需要
Array
(
[C:\] => Array
(
[Users] => Array
(
[Bob] => Array
(
[My Documents] => array()
)
)
)
)
Array
(
[/] => Array
(
[Users] => Array
(
[Bob] => Array
(
[Documents] => array()
)
)
)
)
最终合并到
Array
(
[C:\] => Array
(
[Users] => Array
(
[Bob] => Array
(
[My Documents] => array()
)
)
)
[/] => Array
(
[Users] => Array
(
[Bob] => Array
(
[Documents] => array()
)
)
)
)
(正确合并,不只是附加,所以如果另一个字符串以C:\Users\Dan
开头,那么dan会出现在?3rd?Dimension。array_merge_recursive()
?)
答案 0 :(得分:1)
只需取出preg_split()
返回的数组,然后构建树形结构:
$tree = array();
foreach ( $strings as $string ) {
$path = preg_split( '/(?<=[\/\\\])(?![\/\\\])/', $string );
$ptr =& $tree;
foreach ( $path as $elem ) {
if ( ! array_key_exists( $elem, $ptr ) )
$ptr[ $elem ] = array();
$ptr =& $ptr[ $elem ];
}
}
答案 1 :(得分:0)
你最好只使用pathinfo()
http://uk.php.net/manual/en/function.pathinfo.php
和realpath() http://uk.php.net/manual/en/function.realpath.php
我假设您正在尝试将* nix目录映射到Windows目录?