Solved

Display this multilevel array in php with top level and sub level list items?

Posted on 2014-04-01
12
1,136 Views
Last Modified: 2014-04-03
Any thoughts on how I can display this multilevel array in php with top level and sub level list items?

I am pretty much up to here but only getting top level list items.
<?php 

	 foreach($pagedata as $pk=>$pd){ 
	 
		if ($pd['parent'] == NULL) {

	 ?>
	 
			<li class="<?php echo $page == $pk ? 'page-arrow active-page' : '1' ?>">
				<a href="/?page=<?php echo $pk ?>"><?php echo $pd['title_1'] ?> <?php echo  $pd['icon'] ?></a>
			</li>

	<?php 	
		}
	
	}

?>


<ul>

	<li>Home
    	<ul>
        	<li>Dashboard</li>
            <li>FAQ</li>
            <li>Support</li>
		</ul>
	</li>
    	<li>Account
    	<ul>
        	<li>Profile</li>
            <li>Users</li>
            <li>Financials</li>
		</ul>
	</li>
    
    etc...
</ul>

$pagedata = array(

	
	// home
	
	
	'home' => array(
	
		'id' => '1',

        'title_1' => 'Home',

        'description' => 'Broadcast Network Platform System',

        'filename' => 'dashboard.php',
		
		'parent' => NULL,
		
		'level' => 1),
		
	
	
	'dashbpard' => array(
	
		'id' => '2',

        'title_2' => 'Dashboard',

        'description' => 'This is the Dash',

        'filename' => 'dashboard.php',
		
		'parent' => 1,
		
		'level' => 2),
	
	
	
	'faq' => array(
	
		'id' => '3',

        'title_2' => 'FAQ',

        'description' => 'This is FAQ',

        'filename' => 'faq.php',
		
		'parent' => 1,
		
		'level' => 2),

  	
	
	'support' => array(
	
		'id' => '4',

        'title_2' => 'Support',

        'description' => 'This is support',

        'filename' => 'support.php',
		
		'parent' => 1,
		
		'level' => 2),
	
	
	
	//acount
	
	
	'account' => array(

        'id' => '5',
		
		'title_1' => 'Account',

        'description' => 'This is Account',

        'filename' => 'account.php',
		
		'parent' => NULL,
		
		'level' => 1),

	
	'profile' => array(

        'id' => '6',
		
		'title_2' => 'Profile',

        'description' => 'This is profile',

        'filename' => 'profile.php',
		
		
		'parent' => 5,
		
		'level' => 2),
		
	
    'users' => array(

        'id' => '7',
		
		'title_2' => 'Users',

        'description' => 'This is users',

        'filename' => 'users.php',
		
		'parent' => 5,
		
		'level' => 2),
		
	
	'financials' => array(

        'id' => '8',
		
		'title_2' => 'Financials',

        'description' => 'Account information',

        'filename' => 'account.php',
		
		'parent' => 5,
		
		'level' => 2),
        
        
        etc....
        
);

Open in new window

0
Comment
Question by:morako
  • 6
  • 6
12 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Where did this array come from?  Is it part of a PHP framework?
0
 

Author Comment

by:morako
Comment Utility
No I just created the array..
0
 

Author Comment

by:morako
Comment Utility
It's coming from a php script, but not a framework,  why?
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I was hoping it came from a framework because most PHP frameworks have something like this hierarchy and they use it to generate the site navigation.  If it were from a framework, it might be possible to find the code that turns the array into HTML.
0
 

Author Comment

by:morako
Comment Utility
Not sure if this helps get this closer to a solution, but here it is.  I found a recursive function that loops through parents and childern as such..

Array (
      [1] => Array ( [title_1] => Dashboard [parent] => [id] => 1
              [children] => Array (
                        [2] => Array ( [title_1] => Home [parent] => 1 [id] => 2 )
                           [3] => Array ( [title_1] => FAQ [parent] => 1 [id] => 3 )
                           [4] => Array ( [title_1] => Support [parent] => 1 [id] => 4 ) ) )
                   
        [5] => Array ( [title_1] => Account [parent] => [id] => 5
             [children] => Array (
                         [6] => Array ( [title_1] => Profile [parent] => 5 [id] => 6 )
                    [7] => Array ( [title_1] => Users [parent] => 5 [id] => 7 )
                    [8] => Array ( [title_1] => Financials [parent] => 5 [id] => 8 ) ) )

This is the function:
function buildTree( $ar, $pid = null ) {
							$op = array();
							foreach( $ar as $item ) {
								if( $item['parent'] == $pid ) {
									$op[$item['id']] = array(
										'title_1' => $item['title_1'],
										'parent' => $item['parent'],
										'id' => $item['id']
									);
									// using recursion
									$children =  buildTree( $ar, $item['id'] );
									if( $children ) {
										$op[$item['id']]['children'] = $children;
									}
								}
							}
							return $op;
						}
						
						print_r( buildTree( $pagedata ) );

Open in new window


If this is useful I need help now bringing the foreach statement to work with the array...  outputting list items...

Thanks in advance...    if this doesn't help I am open to other ideas...
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Please do me a favor here.  Put this right before the call to print_r(), then copy and paste the formatted print_r() output into the code snippet here.  I need to see the punctuation to understand the structure.  Thanks!

echo '<pre>';

Open in new window

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:morako
Comment Utility
Array
(
    [1] => Array
        (
            [title_1] => Dashboard
            [parent] => 
            [id] => 1
            [children] => Array
                (
                    [2] => Array
                        (
                            [title_1] => Home
                            [parent] => 1
                            [id] => 2
                        )

                    [3] => Array
                        (
                            [title_1] => FAQ
                            [parent] => 1
                            [id] => 3
                        )

                    [4] => Array
                        (
                            [title_1] => Support
                            [parent] => 1
                            [id] => 4
                        )

                )

        )

    [5] => Array
        (
            [title_1] => Account
            [parent] => 
            [id] => 5
            [children] => Array
                (
                    [6] => Array
                        (
                            [title_1] => Profile
                            [parent] => 5
                            [id] => 6
                        )

                    [7] => Array
                        (
                            [title_1] => Users
                            [parent] => 5
                            [id] => 7
                        )

                    [8] => Array
                        (
                            [title_1] => Financials
                            [parent] => 5
                            [id] => 8
                        )

                )

        )

    [9] => Array
        (
            [title_1] => Media
            [parent] => 
            [id] => 9
            [children] => Array
                (
                    [10] => Array
                        (
                            [title_1] => Library
                            [parent] => 9
                            [id] => 10
                        )

                    [11] => Array
                        (
                            [title_1] => Production
                            [parent] => 9
                            [id] => 11
                        )

                    [12] => Array
                        (
                            [title_1] => Marketplace
                            [parent] => 9
                            [id] => 12
                        )

                )

        )

    [13] => Array
        (
            [title_1] => Content Providers
            [parent] => 
            [id] => 13
            [children] => Array
                (
                    [14] => Array
                        (
                            [title_1] => Earnings
                            [parent] => 13
                            [id] => 14
                        )

                    [15] => Array
                        (
                            [title_1] => content
                            [parent] => 13
                            [id] => 15
                        )

                    [16] => Array
                        (
                            [title_1] => tools
                            [parent] => 13
                            [id] => 16
                        )

                )

        )

    [17] => Array
        (
            [title_1] => Advertisers
            [parent] => 
            [id] => 17
            [children] => Array
                (
                    [18] => Array
                        (
                            [title_1] => Summaries
                            [parent] => 17
                            [id] => 18
                        )

                    [19] => Array
                        (
                            [title_1] => Campaigns
                            [parent] => 17
                            [id] => 19
                        )

                    [20] => Array
                        (
                            [title_1] => solutions
                            [parent] => 17
                            [id] => 20
                        )

                )

        )

    [21] => Array
        (
            [title_1] => Publishers
            [parent] => 
            [id] => 21
            [children] => Array
                (
                    [22] => Array
                        (
                            [title_1] => Activity
                            [parent] => 21
                            [id] => 22
                        )

                    [23] => Array
                        (
                            [title_1] => Products
                            [parent] => 21
                            [id] => 23
                        )

                    [24] => Array
                        (
                            [title_1] => Resouces
                            [parent] => 21
                            [id] => 24
                        )

                )

        )

)

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks.  Let's see if this helps move things forward.  I redacted some of the arrays, but the principles should still apply to the full arrays.  Moving parts start at line 144.
http://iconoun.com/demo/temp_morako.php

<?php // demo/temp_morako.php
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28402060.html#a39974981

$arr =
Array
(
    '1' => Array
        (
              'title_1' => 'Dashboard'
            , 'parent' => ''
            , 'id' => 1
            , 'children' => Array
                (
                    '2' => Array
                        (
                              'title_1' => 'Home'
                            , 'parent' => 1
                            , 'id' => 2
                        ),

                    '3' => Array
                        (
                              'title_1' => 'FAQ'
                            , 'parent' => 1
                            , 'id' => 3
                        ),

                    '4' => Array
                        (
                              'title_1' => 'Support'
                            , 'parent' => 1
                            , 'id' => 4
                        ),

                ),

        ),

    '5' => Array
        (
              'title_1' => 'Account'
            , 'parent' => ''
            , 'id' => 5
            , 'children' => Array
                (
                    '6' => Array
                        (
                              'title_1' => 'Profile'
                            , 'parent' => 5
                            , 'id' => 6
                        ),

                    '7' => Array
                        (
                              'title_1' => 'Users'
                            , 'parent' => 5
                            , 'id' => 7
                        ),

                    '8' => Array
                        (
                              'title_1' => 'Financials'
                            , 'parent' => 5
                            , 'id' => 8
                        ),

                ),

        ),

    '9' => Array
        (
              'title_1' => 'Media'
            , 'parent' => ''
            , 'id' => 9
            , 'children' => Array
                (
                    '10' => Array
                        (
                              'title_1' => 'Library'
                            , 'parent' => 9
                            , 'id' => 10
                        ),

                    '11' => Array
                        (
                              'title_1' => 'Production'
                            , 'parent' => 9
                            , 'id' => 11
                        ),

                    '12' => Array
                        (
                              'title_1' => 'Marketplace'
                            , 'parent' => 9
                            , 'id' => 12
                        ),

                ),

        ),

    '13' => Array
        (
              'title_1' => 'Content Providers'
            , 'parent' => ''
            , 'id' => 13
            , 'children' => Array
                (
                    '14' => Array
                        (
                              'title_1' => 'Earnings'
                            , 'parent' => 13
                            , 'id' => 14
                        ),

                    '15' => Array
                        (
                              'title_1' => 'content'
                            , 'parent' => 13
                            , 'id' => 15
                        ),

                    '16' => Array
                        (
                              'title_1' => 'tools'
                            , 'parent' => 13
                            , 'id' => 16
                        ),

                ),

        ),

)
;

// ACTIVATE THIS TO SEE IF IT LOOKS RIGHT
// print_r($arr);

// FIND THE ELEMENTS
foreach ($arr as $parent)
{
    echo PHP_EOL . $parent['title_1'];
    foreach ($parent['children'] as $child)
    {
        echo PHP_EOL . '  ' . $child['title_1'];
    }
    echo PHP_EOL;
}

Open in new window

0
 

Author Comment

by:morako
Comment Utility
Below is the initial array then the recursive function, and your code...  Not sure how to implement your part, , but this is the whole script to test with...    Thanks in advance...


<?php


$pagedata = array(

	
	// home
	
	
	'dashboard' => array(
	
		'id' => '1',

        'title_1' => 'Dashboard',

        'description' => 'Broadcast Network Platform System',

        'filename' => 'dashboard.php',
		
		'icon' => '<i class="fa fa-dashboard"></i>',
		
		'parent' => NULL,
		
		'level' => 1),
		
	
	
	'home' => array(
	
		'id' => '2',

        'title_1' => 'Home',

        'description' => 'Broadcast Network Platform System',

        'filename' => 'dashboard.php',
		
		'icon' => '<i class="fa fa-dashboard"></i>',
		
		'parent' => 1,
		
		'level' => 2),
	
	
	
	'faq' => array(
	
		'id' => '3',

        'title_1' => 'FAQ',

        'description' => 'Broadcast Network Platform System',

        'filename' => 'dashboard.php',
		
		'icon' => '<i class="fa fa-dashboard"></i>',
		
		'parent' => 1,
		
		'level' => 2),

  	
	
	'support' => array(
	
		'id' => '4',

        'title_1' => 'Support',

        'description' => 'Broadcast Network Platform System',

        'filename' => 'dashboard.php',
		
		'icon' => '<i class="fa fa-dashboard"></i>',
		
		'parent' => 1,
		
		'level' => 2),
	
	
	
	//acount
	
	
	'account' => array(

        'id' => '5',
		
		'title_1' => 'Account',

        'description' => 'Account information',

        'filename' => 'account.php',
		
		
		'icon' => '<i class="fa fa-user"></i>',
		
		'parent' => NULL,
		
		'level' => 1),

	
	'profile' => array(

        'id' => '6',
		
		'title_1' => 'Profile',

        'description' => 'Account information',

        'filename' => 'account.php',
		
		
		'icon' => '<i class="fa fa-user"></i>',
		
		'parent' => 5,
		
		'level' => 2),
		
	'users' => array(

        'id' => '7',
		
		'title_1' => 'Users',

        'description' => 'Account information',

        'filename' => 'account.php',
		
		
		'icon' => '<i class="fa fa-user"></i>',
		
		'parent' => 5,
		
		'level' => 2),
		
	
	'financials' => array(

        'id' => '8',
		
		'title_1' => 'Financials',

        'description' => 'Account information',

        'filename' => 'account.php',
		
		
		'icon' => '<i class="fa fa-user"></i>',
		
		'parent' => 5,
		
		'level' => 2),
	
	
	//media
	
	
	);
	
	
	function buildTree( $ar, $pid = null ) {
		$op = array();
		foreach( $ar as $item ) {
			if( $item['parent'] == $pid ) {
				$op[$item['id']] = array(
					'title_1' => $item['title_1'],
					'parent' => $item['parent'],
					'id' => $item['id']
				);
				// using recursion
				$children =  buildTree( $ar, $item['id'] );
				if( $children ) {
					$op[$item['id']]['children'] = $children;
				}
			}
		}
		return $op;
	}
	
	echo "<pre>";			
								
	print_r( buildTree( $pagedata ) );




	// ACTIVATE THIS TO SEE IF IT LOOKS RIGHT
	// print_r($arr);
	
	// FIND THE ELEMENTS
	foreach ($arr as $parent)
	{
		echo PHP_EOL . $parent['title_1'];
		foreach ($parent['children'] as $child)
		{
			echo PHP_EOL . '  ' . $child['title_1'];
		}
		echo PHP_EOL;
	}
?>

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
See if this looks like what you're wanting.  If you feel like you want to strengthen your PHP skills, you might find some value in the learning resources here:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

<?php // demo/temp_morako.php
error_reporting(E_ALL);
echo '<pre>';

// SNIPPET FROM http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28402060.html

$pagedata = array(
    'dashboard' => array(
        'id' => '1',
        'title_1' => 'Dashboard',
        'description' => 'Broadcast Network Platform System',
        'filename' => 'dashboard.php',
        'icon' => '<i class="fa fa-dashboard"></i>',
        'parent' => NULL,
        'level' => 1),

    'home' => array(
        'id' => '2',
        'title_1' => 'Home',
        'description' => 'Broadcast Network Platform System',
        'filename' => 'dashboard.php',
        'icon' => '<i class="fa fa-dashboard"></i>',
        'parent' => 1,
        'level' => 2),

    'faq' => array(
        'id' => '3',
        'title_1' => 'FAQ',
        'description' => 'Broadcast Network Platform System',
        'filename' => 'dashboard.php',
        'icon' => '<i class="fa fa-dashboard"></i>',
        'parent' => 1,
        'level' => 2),

    'support' => array(
        'id' => '4',
        'title_1' => 'Support',
        'description' => 'Broadcast Network Platform System',
        'filename' => 'dashboard.php',
        'icon' => '<i class="fa fa-dashboard"></i>',
        'parent' => 1,
        'level' => 2),


    'account' => array(
        'id' => '5',
        'title_1' => 'Account',
        'description' => 'Account information',
        'filename' => 'account.php',
        'icon' => '<i class="fa fa-user"></i>',
        'parent' => NULL,
        'level' => 1),

    'profile' => array(
        'id' => '6',
        'title_1' => 'Profile',
        'description' => 'Account information',
        'filename' => 'account.php',
        'icon' => '<i class="fa fa-user"></i>',
        'parent' => 5,
        'level' => 2),

    'users' => array(
        'id' => '7',
        'title_1' => 'Users',
        'description' => 'Account information',
        'filename' => 'account.php',
        'icon' => '<i class="fa fa-user"></i>',
        'parent' => 5,
        'level' => 2),

    'financials' => array(
        'id' => '8',
        'title_1' => 'Financials',
        'description' => 'Account information',
        'filename' => 'account.php',
        'icon' => '<i class="fa fa-user"></i>',
        'parent' => 5,
        'level' => 2),
);


function buildTree( $ar, $pid = null ) {
    $op = array();
    foreach( $ar as $item ) {
        if( $item['parent'] == $pid ) {
            $op[$item['id']] = array(
                'title_1' => $item['title_1'],
                'parent' => $item['parent'],
                'id' => $item['id']
            );
            // using recursion
            $children =  buildTree( $ar, $item['id'] );
            if( $children ) {
                $op[$item['id']]['children'] = $children;
            }
        }
    }
    return $op;
}

$tree = buildTree($pagedata);

echo "<pre>";

// FIND THE ELEMENTS
foreach ($tree as $parent)
{
    echo PHP_EOL . $parent['title_1'];
    foreach ($parent['children'] as $child)
    {
        echo PHP_EOL . '  ' . $child['title_1'];
    }
    echo PHP_EOL;
}

Open in new window

0
 

Author Closing Comment

by:morako
Comment Utility
Thanks for help and input.  i will read article...  ;-)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now