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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:46:20+00:00 2026-05-18T02:46:20+00:00

I have an array of documents, where each document have another simple one-dimensional array

  • 0

I have an array of documents, where each document have another simple one-dimensional array of facets (simple textual labels attached to the document) that have structural value in their order (0 from nearest the root to the edges). I’m traversing this array, and would like to create a multi-dimensional array, like a tree-structure. So, having something like this fragment of just one of those documents ;

Array ( 'document-001' => Array (
   Array ( 'facets' => array (
      'Public - Policy and Procedures',
      'Employment Services Manual',
      'Section 02 - Recruitment & Selection',
   )
   ... many more here ...
) ;

I want this ;

Array
(
    [Public - Policy and Procedures] => Array (
            [Administration Manual] => Array ( )
            [Corporate Governance Manual] => Array ( )
            [Food Services Manual] => Array ( )
            [Charter Manual] => Array ( )
            [Infection Control Manual] => Array ( )
            [Leisure and Lifestyle Manual] => Array ( )
            [Employment Services Manual] => Array (
                    [Section 09 - Termination & Resignation] => Array ( )
                    [Section 02 - Recruitment & Selection] => Array ( )
                    [Section 10 - Security] => Array ( )
            )
            [Environmental Sustainability Manual] => Array (
                    [Property - New Development & Refurbishment Policy 5.5] => Array ( )
            )
     )

My current solution is highly inelegant, where $index is my new multi-dimensional array ;

// Pick out the facets array from my larger $docs array
$t = $docs['facets'] ;
$c = count ( $t ) ;

if      ( $c == 2 ) $index[$t[0]] = array() ;
else if ( $c == 3 ) $index[$t[0]][$t[1]] = array() ;
else if ( $c == 4 ) $index[$t[0]][$t[1]][$t[2]] = array() ;
else if ( $c == 5 ) $index[$t[0]][$t[1]][$t[2]][$t[3]] = array() ;
else if ( $c == 6 ) $index[$t[0]][$t[1]][$t[2]][$t[3]][$t[4]] = array() ;
else if ( $c == 7 ) $index[$t[0]][$t[1]][$t[2]][$t[3]][$t[4]][$t[5]] = array() ;

Surely there’s a better way. I’ve troddled through the various array functions, but nothing stands out as an obvious solution. The problem here is that the dynamicism battles against the syntax of PHP itself. I could of course create an OO solution, but this is such a simple little traverse I don’t really want to go there (even though I probably should).

Thoughts?

  • 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-18T02:46:21+00:00Added an answer on May 18, 2026 at 2:46 am

    Just use some recursion:

    function bar($source, $dest){
        if(count($source) == 0){
            return array();
        }
        $dest[$source[0]] = bar(array_slice($source, 1), $dest[$source[0]]);
        return $dest;
    }
    
    $t = $docsA['facets'];
    $s = $docsB['facets'];
    
    $index = array();
    $index = bar($t, $index);
    $index = bar($s, $index);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.