Give this a go:

<?php
header('content-type: text/plain');

//testdata for your convinience :)
$data = array();
$data[0]['name'] = 'Features';
$data[0]['depth'] = 4;
$data[1]['name'] = 'Administration';
$data[1]['depth'] = 5;
$data[2]['name'] = 'Reliability';
$data[2]['depth'] = 5;
$data[3]['name'] = 'FAQ';
$data[3]['depth'] = 5;
$data[4]['name'] = 'Photos';
$data[4]['depth'] = 4;
$data[5]['name'] = 'More photos';
$data[5]['depth'] = 5;
$data[6]['name'] = 'Even more photos';
$data[6]['depth'] = 6;
$data[7]['name'] = 'Contact';
$data[7]['depth'] = 4;

$newData = array();
$startDepth = $prevDepth = 4;
$stack[$startDepth] =& $newData;
foreach($data as $d) {
     if ($d['depth'] == $prevDepth) {
         /* Same level as the previous, just add it on */
         $stack[$d['depth']][] = $d;
     } elseif ($d['depth'] == $prevDepth + 1) {
         /* Child of the previous */
         $stack[$d['depth']] =& 
$stack[$prevDepth][array_pop(array_keys($stack[$prevDepth]))];
         $stack[$d['depth']][] = $d;
     } elseif ($d['depth'] < $prevDepth) {
         /* Going up one or more levels */
         while ($d['depth'] < $prevDepth) {
             unset($stack[$prevDepth]);
             $prevDepth--;
         }
         $stack[$d['depth']][] = $d;
     }
     $prevDepth = $d['depth'];
}
unset($stack);

print_r($newData);

- Mike

On 9/10/2008 12:39, DW wrote:
> seems to be a bit tricky, and im stuck on this since yesterday:
>
> i have my navigation structure in a database. my query and everything
> is ok, and i get this result:
> (cut down here to keep it simple)
>
> Array
> (
>      [0] =>  Array
>          (
>              [name] =>  Features
>              [depth] =>  4
>          )
>
>      [1] =>  Array
>          (
>              [name] =>  Administration
>              [depth] =>  5
>          )
>
>      [2] =>  Array
>          (
>              [name] =>  Reliability
>              [depth] =>  5
>          )
>
>      [3] =>  Array
>          (
>              [name] =>  Performance
>              [depth] =>  5
>          )
>
>
>      [4] =>  Array
>          (
>              [name] =>  FAQ
>              [depth] =>  4
>          )
>
>
>      [5] =>  Array
>          (
>              [name] =>  Photos
>              [depth] =>  4
>          )
>
>      [6] =>  Array
>          (
>              [name] =>  More photos
>              [depth] =>  5
>          )
>
>      [7] =>  Array
>          (
>              [name] =>  Even more photos
>              [depth] =>  6
>          )
>
> )
>
> depth indicated the 2nd dimension in my menu here. this is my sidebar
> only, so elements with depth 0-3 are already taken care of somewhere
> else.
>
> how can i adjust the array so that everything with depth 5 gets
> attached to the previous depth 4 (and 6 to the previous depth 5 and so
> on) element so that it looks like this one:
>
>      [0] =>  Array
>          (
>              [name] =>  Features
>              [depth] =>  4
>              [0] =>  Array
>                  (
>                      [0] =>  Array
>                              (
>                                  [name] =>  Administration
>                                  [depth] =>  5
>                              )
>
>                      [1] =>  Array
>                              (
>                                  [name] =>  Reliability
>                                  [depth] =>  5
>                              )
>
>                      [2] =>  Array
>                              (
>                                  [name] =>  Performance
>                                  [depth] =>  5
>                              )
>                  )
>              [1] =>  Array
>                  (
>                      [name] =>  FAQ
>                      [depth] =>  4
>                  )
>
>              [2] =>  Array
>                  (
>                      [name] =>  Photos
>                      [depth] =>  4
>                  )
>
>                      [0] =>  Array
>                          (
>                              [name] =>  More photos
>                              [depth] =>  5
>                          )
>
>                              [0] =>  Array
>                                  (
>                                      [name] =>  Even more photos
>                                      [depth] =>  6
>                                  )
>
>
>
> this is what i have so far:
>
> <?php
>
> //testdata for your convinience :)
> $data = array();
> $data[0]['name'] = 'Features';
> $data[0]['depth'] = 4;
> $data[1]['name'] = 'Administration';
> $data[1]['depth'] = 5;
> $data[2]['name'] = 'Reliability';
> $data[2]['depth'] = 5;
> $data[3]['name'] = 'FAQ';
> $data[3]['depth'] = 5;
> $data[4]['name'] = 'Photos';
> $data[4]['depth'] = 4;
> $data[5]['name'] = 'More photos';
> $data[5]['depth'] = 5;
> $data[6]['name'] = 'Even more photos';
> $data[6]['depth'] = 6;
>
> function setNestedMenu($data, $init_depth = 4)
> {
>      //thats pretty fu now...
>      $x=0;
>      foreach($data as $i=>$val)
>      {
>          echo $i." - ".$init_depth." - ".$val['depth']." - ".
> $val['name']."<br>";
>          if($val['depth']==$init_depth)
>          {
>              $this->newMenu[] = $val;
>              unset($data[$i]);
>              $x++;
>          }
>          if($val['depth']>$init_depth)
>          {
>              $i = $x-1;
>              $this->newMenu[$i][] = $this->setNestedMenu($data,
> $val['depth']);
>              unset($data[$i]);
>
>          }
>          if($val['depth']<$init_depth)
>          {
>              return $data;
>          }
>      }
> }
>
> ?>
>
> anyone who can still follow me with a hint to point me into the right
> direction here?
>
> >


-- 
Mike Cochrane
Web Team Leader

gardyneHOLT - design partners
18 Beresford Square Newton
PO Box 3340 Auckland New Zealand
p +64 9 300 3155  f +64 9 302 3349  m 021 545 565
skype gardyneholt_mikec
www.gardyneholt.co.nz


--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[EMAIL PROTECTED]
-~----------~----~----~----~------~----~------~--~---

Reply via email to