Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-04-01
12
Medium Priority
?
1,294 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 111

Expert Comment

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

Author Comment

by:morako
ID: 39969230
No I just created the array..
0
 

Author Comment

by:morako
ID: 39969260
It's coming from a php script, but not a framework,  why?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39969270
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
ID: 39970569
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 111

Expert Comment

by:Ray Paseur
ID: 39974956
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
 

Author Comment

by:morako
ID: 39974981
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 111

Expert Comment

by:Ray Paseur
ID: 39975088
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
ID: 39975217
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 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 39975486
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
ID: 39975639
Thanks for help and input.  i will read article...  ;-)
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 39976094
Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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 …
Suggested Courses
Course of the Month20 days, 23 hours left to enroll

810 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