Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:27:15+00:00 2026-05-26T01:27:15+00:00

Hello just looking for some help as I’ve gotten stuck I have two Strings:

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:27:15+00:00Added an answer on May 26, 2026 at 1:27 am

    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 ];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone I was just going trough http://static.springsource.org/docs/Spring-MVC-step-by-step/part1.html spring tutorial, and I thought its
Hello guys. I think it isn't possible just using PHP, but just to be
I just installed Ubuntu 8.04 and Eclipse. I made a very simple Hello World
Just a blackout, I want the String hello hallo tjena hej tere to an
I'm just starting with Adobe Flex on linux, and can't get the hello world
Hello I have the following error by git-fsck, which cannot be cleaned by git-gc
This is currently a homework project that me and my teammate are stuck on.
Hello I have a ASPxGridView. In it(for the uninformed) is only a DataSource property
A friend of mine asked me the other day if I'm just looking at
I am looking for a tool that formats my javascript for me. There are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.