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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:11:45+00:00 2026-05-27T21:11:45+00:00

I’m not even sure how to begin wording this question, but basically, I have

  • 0

I’m not even sure how to begin wording this question, but basically, I have an array, that looks like this:

Array
(
    [0] => /
    [1] => /404/
    [2] => /abstracts/
    [3] => /abstracts/edit/
    [4] => /abstracts/review/
    [5] => /abstracts/view/
    [6] => /admin/
    [7] => /admin/ads/
    [8] => /admin/ads/clickcounter/
    [9] => /admin/ads/delete/
    [10] => /admin/ads/edit/
    [11] => /admin/ads/list/
    [12] => /admin/ads/new/
    [13] => /admin/ads/sponsordelete/
    [14] => /admin/ads/sponsoredit/
    [15] => /admin/ads/sponsornew/
    [16] => /admin/ads/stats/
    [17] => /admin/boilerplates/
    [18] => /admin/boilerplates/deleteboiler/
    [19] => /admin/boilerplates/editboiler/
    [20] => /admin/boilerplates/newboilerplate/
    [21] => /admin/calendar/event/add/
    [22] => /admin/calendar/event/copy/
)

And I need to ‘reduce’ / ‘process’ it into an array that looks like this:

Array
(
    [''] => Array()
    ['404'] => Array()
    ['abstracts'] => Array
                     (
                          [''] => Array()
                          ['edit'] => Array()
                          ['review'] => Array()
                          ['view'] => Array()
                     )
    ['admin'] => Array
                     (
                          ['ads'] => Array
                                     (
                                         [''] => Array()
                                         ['clickcounter'] => Array()
                                         ['delete'] =>Array()
                                         ['edit'] => Array()
                                     )
                     )
    .....
    .....   
)

That, if manually initialized would look something like this:

$urlTree = array( ''         => array(),
                  '404'      => array(),
                  'abstracts'=> array( ''      => array(),
                                       'edit'  => array(),
                                       'review'=> array(),
                                       'view'  => array() ),
                  'admin'    => array( 'ads'=> array( ''            => array(),
                                                      'clickcounter'=> array(),
                                                      'delete'      => array(),
                                                      'edit'        => array() ) )
);

I usually stray away from asking straight up for a chunk of code on SO, but does anyone perhaps have any advice / code that can traverse my array and convert it to a hierarchy?

EDIT: Here is the bit I have right now, which, I know is pitifully small, I’m just blanking out today it seems.

  function loadUrlData()
  {
    // hold the raw data, /blah/blah/
    $urlData = array();

    $res = sql::query( "SELECT DISTINCT(`url`) FROM `pages` ORDER BY `url` ASC" );
    while( $row = sql::getarray( $res ) )
    {
      $urlData[] = explode( '/', substr( $row['url'], 1, -1 ) );
    }

    // populated, eventually, with the parent > child data
    $treeData = array();

    // a url
    foreach( $urlData as $k=> $v )
    {
      // the url pieces
      foreach( $v as $k2=> $v2 )
      {

      }
    }

    // $treeData eventually
    return $urlData;
  }
  • 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-27T21:11:46+00:00Added an answer on May 27, 2026 at 9:11 pm

    Looks rather easy. You want to loop through all lines (foreach), split them into parts (explode), loop through them (foreach) and categorize them.

    Since you don’t like asking for a chunk of code, I won’t provide any.

    Update
    A very nice way to solve this is to reference the $urlTree (use &), loop through every part of the URL and keep updating a variable like $currentPosition to the current part in the URL tree. Because you use &, you can simply edit the array directly while still using a simple variable.

    Update 2
    This might work:

    // a url
    foreach( $urlData as $k=> $v )
    {
      $currentSection = &$treeData;
      // the url pieces
      foreach( $v as $k2=> $v2 )
      {
        if (!isset($currentSection[$v2])) {
          $currentSection[$v2] = array();
        }
    
        $currentSection = &$currentSection[$v2];
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need a function that will clean a strings' special characters. I do NOT
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.