Josh
Josh

Reputation: 12566

PHP - Create Hierarchal Array

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;
  }

Upvotes: 0

Views: 1054

Answers (6)

piotrekkr
piotrekkr

Reputation: 3206

My version:

$paths = array(
    0 => '/',
    1 => '/404/',
    2 => '/abstracts/',
    3 => '/abstracts/edit/',
    4 => '/abstracts/review/',
    5 => '/abstracts/view/',
    6 => '/admin/',
    7 => '/admin/ads/',
    // ....
);
$tree = array();
foreach($paths as $path){
    $tmp = &$tree;
    $pathParts = explode('/', rtrim($path, '/'));
    foreach($pathParts as $pathPart){
        if(!array_key_exists($pathPart, $tmp)){
            $tmp[$pathPart] = array();
        }
        $tmp = &$tmp[$pathPart];
    }
}

echo json_encode($tree, JSON_PRETTY_PRINT);

https://ideone.com/So1HLm

Upvotes: 2

Bailey Parker
Bailey Parker

Reputation: 15903

I know you didn't ask for a chunk of code, but I'd just call this a petit serving:

$map = array();
foreach($urls as $url) {
    $folders = explode('/', trim($url, '/'));

    applyChain($map, $folders, array());
}

function applyChain(&$arr, $indexes, $value) { //Here's your recursion
    if(!is_array($indexes)) {
        return;
    }

    if(count($indexes) == 0) {
        $arr = $value;
    } else {
        applyChain($arr[array_shift($indexes)], $indexes, $value);
    }
}

It's fairly simple. We separate each url into its folders (removing trailing and leading slashes) and then work our way down the array chain until we reach the folder mentioned in the URL. Then we place a new empty array there and continue to the next URL.

Upvotes: 2

Mikko
Mikko

Reputation: 602

<?php
$old_array = array("/", "/404/", "/abstracts/", "/abstracts/edit/", "/abstracts/review/", "/rrl/");
$new_array = array();

foreach($old_array as $woot) {
    $segments = explode('/', $woot);
    $current = &$new_array;
    for($i=1; $i<sizeof($segments); $i++) {
        if(!isset($current[$segments[$i]])){
            $current[$segments[$i]] = array();
        }
        $current = &$current[$segments[$i]];
    }
}

print_r($new_array);

?>

Upvotes: 1

dar7yl
dar7yl

Reputation: 3757

You might consider converting your text to a JSON string, then using json_decode() to generate the structure.

Upvotes: 0

zerkms
zerkms

Reputation: 255095

http://ideone.com/S9pWw

$arr = array(
    '/',
    '/404/',
    '/abstracts/',
    '/abstracts/edit/',
    '/abstracts/review/',
    '/abstracts/view/',
    '/admin/',
    '/admin/ads/',
    '/admin/ads/clickcounter/',
    '/admin/ads/delete/',
    '/admin/ads/edit/',
    '/admin/ads/list/',
    '/admin/ads/new/',
    '/admin/ads/sponsordelete/',
    '/admin/ads/sponsoredit/',
    '/admin/ads/sponsornew/',
    '/admin/ads/stats/',
    '/admin/boilerplates/',
    '/admin/boilerplates/deleteboiler/',
    '/admin/boilerplates/editboiler/',
    '/admin/boilerplates/newboilerplate/',
    '/admin/calendar/event/add/',
    '/admin/calendar/event/copy/');

$result = array();
foreach ($arr as $node) {
    $result = magic($node, $result);
}

var_dump($result);

function magic($node, $tree)
{
    $path = explode('/', rtrim($node, '/'));

    $original =& $tree;

    foreach ($path as $node) {
        if (!array_key_exists($node, $tree)) {
            $tree[$node] = array();
        }

        if ($node) {
            $tree =& $tree[$node];
        }
    }

    return $original;
}

Upvotes: 1

Tom van der Woerdt
Tom van der Woerdt

Reputation: 29985

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];
  }
}

Upvotes: 2

Related Questions