preg_split 字符串到多维数组

发布于 2024-12-08 06:51:12 字数 1658 浏览 0 评论 0原文

你好,只是在寻找一些帮助,因为我陷入了困境,

我有两个字符串:

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 将出现在第 3 个维度上。

Hello just looking for some help as I've gotten stuck

I have two Strings:

C:\Users\Bob\My Documents
/Users/Bob/Documents

That gets put through

preg_split('/(?<=[\/\\\])(?![\/\\\])/', $string)

that returns

Array
(
    [0] => C:\
    [1] => Users\
    [2] => Bob\
    [3] => My Documents
)

Array
(
    [0] => /
    [1] => Users/
    [2] => Bob/
    [3] => Documents
)

I need

Array
(
    [C:\] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [My Documents] => array()
                        )

                )

        )

)

Array
(
    [/] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [Documents] => array()
                        )

                )

        )

)

And ultimately merged to

Array
(
    [C:\] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [My Documents] => array()
                        )

                )

        )
    [/] => Array
        (
            [Users] => Array
                (
                    [Bob] => Array
                        (
                            [Documents] => array()
                        )

                )

        )

)

(properly merged, not just appended, so if another string started with C:\Users\Dan Then dan would appear on the ?3rd? Dimension. array_merge_recursive() ? )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒冷纷飞旳雪 2024-12-15 06:51:12

只需获取 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 ];
    }
}

Just take the arrays returned by preg_split() and build your tree structure out of them:

$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 ];
    }
}
陪你搞怪i 2024-12-15 06:51:12

您可能最好只使用 pathinfo()

http://uk.php。 net/manual/en/function.pathinfo.php

和 realpath()
http://uk.php.net/manual/en/function.realpath.php

我假设您正在尝试将 *nix 目录映射到 Windows 目录?

You're probably best off just using pathinfo()

http://uk.php.net/manual/en/function.pathinfo.php

And realpath()
http://uk.php.net/manual/en/function.realpath.php

I assume you're trying to map a *nix directory to a Windows one?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文