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

  • SEARCH
  • Home
  • 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 710355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:36:41+00:00 2026-05-14T04:36:41+00:00

I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>

  • 0

I have array like this:

$path = array (
[0] => site\projects\terrace_and_balcony\mexico.jpg
[1] => site\projects\terrace_and_balcony\new_york.jpg
[2] => site\projects\terrace_and_balcony\berlin.jpg
[3] => site\projects\terrace_and_balcony\Kentucky.jpg
[4] => site\projects\terrace_and_balcony\Utah.jpg
[5] => site\projects\terrace_and_balcony\Hawaii.jpg
[6] => site\projects\private_gardens\mexico.jpg
[7] => site\projects\private_gardens\new_york.jpg
[8] => site\projects\private_gardens\berlin.jpg
[9] => site\projects\private_gardens\Kentucky.jpg
[10] => site\projects\private_gardens\Utah.jpg
[11] => site\projects\private_gardens\Hawaii.jpg
)

How to convert it to that:

$path11 = array
(
    "site"=>array
    (
        "projects"=>array
        (
            "terrace_and_balcony"=>array
            (
                "mexico.jpg",
                "new_york.jpg",
                "berlin.jpg",
                "Kentucky.jpg",
                "Utah.jpg",
                "Hawaii.jpg"
            ),
            "private_gardens"=>array
            (
                "mexico.jpg",
                "new_york.jpg",
                "berlin.jpg",
                "Kentucky.jpg",
                "Utah.jpg",
                "Hawaii.jpg"
            )
        )
    )
);
  • 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-14T04:36:41+00:00Added an answer on May 14, 2026 at 4:36 am
    $pathStrings = [
        'site\\projects\\terrace_and_balcony\\mexico.jpg',
        'site\\projects\\terrace_and_balcony\\new_york.jpg',
        'site\\projects\\terrace_and_balcony\\berlin.jpg',
        'site\\projects\\terrace_and_balcony\\Kentucky.jpg',
        'site\\projects\\terrace_and_balcony\\Utah.jpg',
        'site\\projects\\terrace_and_balcony\\Hawaii.jpg',
        'site\\projects\\private_gardens\\mexico.jpg',
        'site\\projects\\private_gardens\\new_york.jpg',
        'site\\projects\\private_gardens\\berlin.jpg',
        'site\\projects\\private_gardens\\Kentucky.jpg',
        'site\\projects\\private_gardens\\Utah.jpg',
        'site\\projects\\private_gardens\\Hawaii.jpg'
    ];
    
    $paths = [];
    foreach ($pathStrings as $pathString) {
        // Split by the delimiter.
        $pathParts = explode('\\', $pathString);
    
        // Build a nested assoc array representing the path.
        // Each key and value comes from the delimited parts of the string.
        // eg: site\projects\terrace_and_balcony\mexico.jpg
        // becomes: [
        //      'site' => [
        //              'projects' => [
        //                      'terrace_and_balcony' => [
        //                              'mexico.jpg'
        //                      ]
        //              ]
        //      ]
        // ]
        $path = [array_pop($pathParts)];
        foreach (array_reverse($pathParts) as $pathPart) {
            $path = [$pathPart => $path];
        }
    
        // Add it to a temp list.
        $paths[] = $path;
    }
    
    // Now, merge all the paths together recursively at once.
    $tree = call_user_func_array('array_merge_recursive', $paths);
    
    // Done.
    print_r($tree);
    

    Demo: http://sandbox.onlinephpfunctions.com/code/12c82c18625dda23682baac37b2a5a8310fb869d

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of file-path strings like this videos/funny/jelloman.wmv videos/funny/bellydance.flv videos/abc.mp4 videos/june.mp4 videos/cleaver.mp4
I have an array of image files with relative paths, like this: gallery/painting/some_image_name.jpg .
I have a string like this: C:\Projects\test\whatever\files\media\10\00\00\80\test.jpg Now, what I want to do is
If I have something like this: PROJECTS += path/to/first PROJECTS += path/to/second PROJECTS +=
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
I have an array like this: object[] args and need to insert those args
I have an array like this: $sports = array( 'Softball - Counties', 'Softball -
I have an array like this: var arr1 = [a, b, c, d]; How
I have an array like this: Array ( [23] => 540, [25] => Array

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.