>
 > 1. sort whole array by depth.
 > 2. attaching elements one by one. (think sorted array as a queue,keep
 > tracking depth here)
 >


if you sort array by depth you will lose relationship between parent and
child nodes.

 >> anyone who can still follow me with a hint to point me into the right
 >> direction here?

you can try using stack of parents instead of recursion:


$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'] = 6;
$data[3]['name'] = 'FAQ';
$data[3]['depth'] = 7;
$data[4]['name'] = 'Photos';
$data[4]['depth'] = 8;
$data[5]['name'] = 'More photos';
$data[5]['depth'] = 9;
$data[6]['name'] = 'Even more photos';
$data[6]['depth'] = 5;

$nested_data = array();

for ($i = 0; $i < count($data); $i++)
{
     if (!isset($prev_item))
     {
         $parents = array();
         $parents[] = &$nested_data;
     }
     elseif ($data[$i]['depth'] > $prev_item['depth'])
     {
         $parents[] = &$prev_item;
     }
     elseif ($data[$i]['depth'] < $prev_item['depth'])
     {
         while ($parents[count($parents)-1]['depth'] >
                $data[$i]['depth'])
         {
             array_pop($parents);
         }
     }

     $parents[count($parents)-1][] = &$data[$i];
     $prev_item = &$data[$i];
}

print_r($nested_data);




ctx2002 wrote:
> 
> 1. sort whole array by depth.
> 2. attaching elements one by one. (think sorted array as a queue,keep
> tracking depth here)
> 
> same thing as breadth first tree traveling.
> 
> best way is just to use nested set in your database modeling.
> searching google to find more detail.
> 
> reagrds
> 
> On Oct 9, 12:39 pm, DW <[EMAIL PROTECTED]> 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?
> > 

--~--~---------~--~----~------------~-------~--~----~
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