tags:

views:

245

answers:

2

Dear all,

just want to ask anyone here can solve this problem?

I want to create a multilevel menu by generate with multilevel array. I just can't get perfect solution, it always got array_push error. My study the original idea come from http://www.weberdev.com/get_example-4830.html

But still cannot match my need.

This is My MySQL

[code]
-----------------------------------------
|id | parent_id| name    | link     |seq
|1  |  0       |dashboad |dashboard/|1
|2  |  0       |menu1    |menu1/    |2
|3  |  0       |menu2    |menu2/    |3
|4  |  0       |menu3    |menu3/    |4
|5  |  2       |add      |add/      |1
|6  |  3       |add      |add/      |1
|7  |  2       |edit     |edit/     |2
|8  |  4       |add      |add/      |1
------------------------------------------
[/code]

I want my array some thing like this

array('dashboard'=>array(
                         'id'=>'',
                         'name'=>'dashboard',
                         'title'=>'dashboard',
                         'permalink'=>base_url().'dashboard/',
                         'active'=>FALSE,
                         'child'=>NULL
                         ),
     'menu1'=>array(
                         'id'=>'',
                         'name'=>'menu1',
                         'title'=>'menu1',
                         'permalink'=>base_url().'menu1/',
                         'active'=>FALSE,
                         'child'=>array(
                                       'add'=>array(
                                                     'id'=>'',
                                                     'name'=>'add',
                                                     'title'=>'add',
                                                     'permalink'=>base_url().'menu1/add',
                                                     'active'=>FALSE,
                                                     'child'=>NULL
                                                     ),
                                       'edit'=>array(
                                                     'id'=>'',
                                                     'name'=>'edit',
                                                     'title'=>'edit',
                                                     'permalink'=>base_url().'menu1/edit/',
                                                     'active'=>FALSE,
                                                     'child'=>NULL
                                                     )
                                                   ),
                                 ),
      ...................[similar like above]
                                       );       

I only able to do until push_array, but it has warning Warning: array_push() [function.array-push]: First argument should be an array

I have this question had been discuss with a lot, but after I do some research most of the case are doing multilevel menu but in one level array. I want to achieve something multilevel array. Anyone can help?

The temporary array I am using static type.

A: 

[Solved]

I have to declare the array first before I push it, else PHP can't push the data inside, it will be empty. Praise the Lord!

if(!isset($option_results[$nav_ParentName]['child']))
                    {
                        $option_results[$nav_ParentName]['child'] = array();  
                    }
                    $option_results[$nav_ParentName]['child'] = $option_results[$nav_ParentName]['child'] + $temp;

but the thing for this solution is cannot declare $array['child'] = null;

Shiro
+2  A: 

I would advise you have a look at

http://www.ideashower.com/our_solutions/create-a-parent-child-array-structure-in-one-pass/ & http://www.phpriot.com/articles/nested-trees-1

Between those two ideas you can get a very workable array tree going to build whatever you need.

devians
thank you devians! The array structure really impress!
Shiro