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 7064585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:51:09+00:00 2026-05-28T04:51:09+00:00

I was surprised not to find an answer to this on SO (or elsewhere

  • 0

I was surprised not to find an answer to this on SO (or elsewhere on the internet for that matter). It concerns a nested indented list which I want to convert into a multidimensional array according to the level of indentation.

By way of an example, here is some sample input:

Home
Products
    Product 1
        Product 1 Images
    Product 2
        Product 2 Images
    Where to Buy
About Us
    Meet the Team
    Careers
Contact Us

Ideally I’d like to feed this into some (recursive?) function and get the following output:

array(
    'Home' => array(),
    'Products' => array(
        'Product 1' => array(
            'Product 1 Images' => array(),
        ),
        'Product 2' => array(
            'Product 2 Images' => array(),
        ),
        'Where to Buy' => array(),
    ),
    'About Us' => array(
        'Meet the Team' => array(),
        'Careers' => array(),
    ),
    'Contact Us' => array(),
);

I’m confused by the logic required to perform such a task, so any help would be appreciated.

  • 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-28T04:51:09+00:00Added an answer on May 28, 2026 at 4:51 am

    As it’s still unclear if you’re trying to read from some given structure (html-dom) or from the given string as plain text, I assumed it’s the string you’re trying to parse. If so, try:

    <?php
    $list =
    'Home
    Products
        Product 1
            Product 1 Images
        Product 2
            Product 2 Images
        Where to Buy
    About Us
        Meet the Team
        Careers
    Contact Us';
    
    function helper($list, $indentation = '    ') {
      $result = array();
      $path = array();
    
      foreach (explode("\n", $list) as $line) {
        // get depth and label
        $depth = 0;
        while (substr($line, 0, strlen($indentation)) === $indentation) {
          $depth += 1;
          $line = substr($line, strlen($indentation));
        }
    
        // truncate path if needed
        while ($depth < sizeof($path)) {
          array_pop($path);
        }
    
        // keep label (at depth)
        $path[$depth] = $line;
    
        // traverse path and add label to result
        $parent =& $result;
        foreach ($path as $depth => $key) {
          if (!isset($parent[$key])) {
            $parent[$line] = array();
            break;
          }
    
          $parent =& $parent[$key];
        }
      }
    
      // return
      return $result;
    }
    
    print_r(helper($list));
    

    Demo: http://codepad.org/zgfHvkBV

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

Sidebar

Related Questions

I was surprised not to find an answer to this question, maybe is something
I'm surprised that I could not find any info on this. I must be
I'm actually surprised that I didn't find the answer to this simple question here.
Surprised that i havent been able to find this myself, but anyway. Let's say
I was surprised not to find the answer so far. If I am missing
I figured this was a common scenario and was surprised not to find an
I'm surprised I couldn't find an answer to this anywhere. I'm writing a roguelike
I am surprised I could not find this question already asked, so if I
I am surprised I could not find a "batch" function that would take as
I did a quick search for this and was surprised not to find it

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.