morako
asked on
Display this multilevel array in php with top level and sub level list items?
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.
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....
);
Where did this array come from? Is it part of a PHP framework?
ASKER
No I just created the array..
ASKER
It's coming from a php script, but not a framework, why?
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.
ASKER
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:
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...
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 ) );
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...
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>';
ASKER
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
)
)
)
)
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
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;
}
ASKER
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;
}
?>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for help and input. i will read article... ;-)
Thanks for the points and thanks for using EE, ~Ray