iterating over multi dimensional array for a menu - php

Hello I have a menu made in codeigniter. but I also want this to have submenu's
Therefore I get an array and go through it with a foreach loop.
<ul>
<?php foreach ($menu_item as $menu =>& $key): ?>
<li><?php echo anchor($menu, $key, $this->uri->slash_segment(1, 'leading') == $menu ? 'class="active"' : '') ?></li>
<?php endforeach ?>
</ul>
Now the problem is that this works great if its just one menu without submenu's but when I get an array like this
$menu_item = array(
'/' => 'Home',
'/about' => 'About',
'/foo' => 'boo',
'/contact' => 'contact',
'test' => array(
'foo' => 'boo'
),
'test2' => 'foo2'
);
Than it doesn't work anymore. How can I loop through everything and output it as a good menu?

You can use recursion to do that job. It takes a bit of getting your head around if you're not familiar with it, but it's very well suited to this kind of problem.
I haven't run this code in PHP, but it will give you an idea.
Basically what happens is that the main menu function checks each item to see if it's an array, and then calls the function again using the sub menu. This will work infinitely deep if required.
<?php
$menu = array(
'/' => 'Home',
'/about' => 'About',
'/foo' => 'boo',
'/contact' => 'contact',
'test' => array(
'foo' => 'boo'
),
'test2' => 'foo2'
);
?>
<ul>
<?php showMenu($menu); ?>
</ul>
<?php
function showMenu($menu)
{
<?php foreach ($menu_item as $menu =>& $key): ?>
<li><?php echo anchor($menu, $key, $this->uri->slash_segment(1, 'leading') == $menu ? 'class="active"' : '') ?></li>
if(is_array($menu_item))
{
echo "<ul>";
showMenu($menu_item);
echo "</ul>";
}
<?php endforeach ?>
}
?>
Hope this helps.

The concept of the other answers is true, but they generate invalid DOM structure, so I decided to fix it.
You can make a helper file and put the drawMenu() function inside. So, you'll be able to call the function as much as you need.
$menu = array(
'/' => 'Home',
'/about' => 'About',
'/foo' => 'boo',
'/contact' => 'contact',
'test' => array(
'foo' => 'bar',
'baz' => 'qux'
),
'test2' => 'foo2'
);
function drawMenu($menu)
{
$CI =& get_instance();
$output = '';
foreach ($menu as $key => $value) {
$output .= "<li>";
if (is_array($value)) {
$output .= anchor('#', $key);
$output .= PHP_EOL."<ul>".PHP_EOL;
$output .= drawMenu($value);
$output .= "</ul>".PHP_EOL."</li>".PHP_EOL;
} else {
$output .= anchor($key, $value, $CI->uri->slash_segment(1, 'leading') == $key ? 'class="active"' : '');
$output .= "</li>".PHP_EOL;
}
}
return $output;
}
$html = drawMenu($menu);
echo '<ul>'. $html .'</ul>';
Side-note: Usage PHP_EOL constant is arbitrary, it just makes generated DOM more readable.
Update:
I improved the drawMenu() functionality, now you can add a URL address for the headers of sub-menus:
$menu = array(
'/' => 'Home',
'/about' => 'About',
'/foo' => 'boo',
'/contact' => 'contact',
'test' => array(
'foo' => 'bar'
),
'This is Test2|/url/to/test2' => array(
'baz' => 'qux'
)
);
You can add the URL after | separator.
function drawMenu($menu)
{
$CI =& get_instance();
$output = '';
foreach ($menu as $key => $value) {
$output .= "<li>";
if (is_array($value)) {
if (strpos($key, '|') !== false) {
$param = explode('|', $key);
$output .= anchor($param[1], $param[0]);
} else {
$output .= anchor('#', $key);
}
$output .= PHP_EOL."<ul>".PHP_EOL;
$output .= drawMenu($value);
$output .= "</ul>".PHP_EOL."</li>".PHP_EOL;
} else {
$output .= anchor($key, $value, $CI->uri->slash_segment(1, 'leading') == $key ? 'class="active"' : '');
$output .= "</li>".PHP_EOL;
}
}
return $output;
}

You can check if the $key is an array: is_array
Then you can use another foreach to loop through the submenus.

try this
<ul>
<?php function buildmenu($menu_item){ ?>
<?php foreach($menu_item as $item){ ?>
<li><?php echo anchor($menu, $key, $this->uri->slash_segment(1, 'leading') == $menu ? 'class="active"' : '') ?></li>
<?php if(is_array($item)){
buildmenu($item);
} ?>
<?php } ?>
<php} ?>
<?php buildmenu($menu_item) ?>
</ul>

$menu = "<ul>\n";
foreach ($menu_item as $key => $value){
if (is_array($value)){
$menu.= "\t<li>".$key."\n\t\t<ul>\n";
foreach ($value as $key2 => $value2){
$menu .= "\t\t\t<li>".$value2."</li>\n";
}
$menu.= "\t\t</u>\n\t</li>\n";
} else {
$menu .= "\t<li>".$value."</li>\n";
}
}
$menu .= "</ul>";
echo $menu;
Output:
<ul>
<li>Home</li>
<li>About</li>
<li>boo</li>
<li>contact</li>
<li>test
<ul>
<li>boo</li>
</u>
</li>
<li>foo2</li>
</ul>

Related

PHP Array Iterator to DIV

I want to loop through the unknown depth array with RecursiveIteratorIterator in SELF::FIRST mode along with RecursiveArrayIterator.
If the array value is an array, I will open a DIV so the "subarray" will be inside this DIV. Something like
$array = array(
'key0' => '0',
'key1' => array(
'value0' => '1',
'value1' => '2',
),
'key2' => '3',
'key3' => array(
'value2' => '4',
'value3' => array(
'value4' => '5'
),
'value4' => array(
'value5' => '6'
),
),
);
Then the HTML should be:
<div>
<div>
<p>key0 is 0</p>
</div>
<div>
<p>key1</p>
<div>
<p>value0 is 1</p>
<p>value1 is 2</p>
</div>
</div>
<div>
<p>key2 is 3</p>
</div>
<div>
<p>key3</p>
<div>
<p>value2 is 4</p>
<p>value3</p>
<div>
<p>value4 is 5</p>
</div>
<p>value4</p>
<div>
<p>value5 is 6</p>
</div>
</div>
</div>
</div>
But the problem is my code can only close 1 <div> tag each time. I have no idea how to remember how deep was there. So I can close to a for loop and echo </div>.
My current code:
<?php
echo '<div>';
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator($iterator_array), RecursiveIteratorIterator::SELF_FIRST);
$is_start = true;
$last_element = '';
foreach($iterator as $key => $value) {
if(is_array($value) && $is_start) {
echo '<div><p>' . $key . '</p>';
$is_start = false;
$last_element = end($value);
} elseif(is_array($value) && !$is_start) {
echo '</div><div><p>' . $key . '</p>';
$last_element = end($value);
} elseif(!is_array($value)) {
echo '<div><p>' . $key . ' is ' . $value . '</p></div>';
if($last_element == $value) {
echo '</div>';
}
}
}
echo '</div>';
?>
Use this recursive function
May be it will help you
get_div($array);
function get_div($arr) {
foreach($arr as $k => $a){
echo '<div>';
if(is_array($a)) {
echo "<p>".$k."</p>";
get_div($a);
} else {
echo "<p>".$k." is ".$a."</p>";
}
echo '</div>';
}
}

How can I loop over this multi dimensional array?

So Im trying to loop over an array of categories and sub-categories but Im having trouble with the foreach loop. Once I've got the array working Im going to create a menu where if you hover over a category, sub categories appear.
Put it this way, Im hopeless at the logic side of things :|
This is my array along with the loops I've tried:
$categories = array(
'arr' => array(
'cat'=>'sport',
'subcats'=> array(
"surfing", "basketball", "nfl", "afl"
)
),
'arr' => array(
'cat'=>'Automotive',
'subcats'=> array(
"cars", "f1", "bikes", "etc"
)
),
'arr' => array(
'cat'=>'comedy',
'subcats'=> array(
"stand up", "pranks", "...", "test"
)
)
);
//Loop1
foreach ($categories as $key => $value) {
if($key == "arr"){
foreach ($key as $key => $value) {
echo $value;
if($key == "cat"){
echo "cat";
}
else{
echo $value;
}
}
// echo $key . "<br />";
// if($key == 'subcats'){
// echo "________".$key."<br />";
// }
}
}
//Attempt number 2
foreach ($categories as $key => $value) {
if($key == "arr"){
while($key){
echo $value;
if($key == "cat"){
echo $value;
}
if($key == "subcats"){
while($key){
echo $value[$key];
}
}
}
}
}
Updated code that I tried after this
This works okay but only problem is that it will only loop through the last arr=>array() and not any of the ones before.
$i=0;
foreach ($categories as $key => $value) {
if($key == "arr"){
foreach ($categories['arr'] as $sub => $val) {
if($sub == "cat"){
echo $val . " => ";
}
else if($sub == "subcats"){
for($i=0; $i<4; $i++){
echo $val[$i] . ", ";
}
}
}
}
}
If you can reorganize your array, you can stick the categories as keys, subcategories as values:
$categories = array(
'sport' => array(
"surfing", "basketball", "nfl", "afl"
),
'Automotive' => array(
"cars", "f1", "bikes", "etc"
),
'comedy' => array(
"stand up", "pranks", "...", "test"
)
);
// Here is an imploded list, you don't need a second loop then
foreach($categories as $cat => $subcat) {
echo "<h1>".$cat."</h1>";
echo '<ul>';
echo '<li>'.implode('</li>'.PHP_EOL.'<li>',$subcat).'</li>';
echo '</ul>';
}

Dynamic menu from PHP array

Hey I have question about Dynamic menu which is created in php.
Code is from stackoverflow, what I want is to get my parent styled with red color if children of those parent is selected, here is code:
$menu = Array(
Array(
'title' => 'Home',
'link' => 'a'
),
Array(
'title' => 'Parent',
'link' => 'b',
'children' => Array(
Array(
'title' => 'Sub 1',
'link' => 'c'
),
Array(
'title' => 'Sub 2',
'link' => 'd'
),
)
)
);
function buildMenu($menuArray)
{
foreach ($menuArray as $node)
{
$selected = ($node['link']== $_GET['menu']) ? $selected = 'style="color: red;"' : null;
echo "<li ".$selected."><a href='?menu=".$node['link']."'/>" . $node['title'] . "</a>";
if ( ! empty($node['children'])) {
echo "<ul>";
buildMenu($node['children']);
echo "</ul>";
}
echo "</li>";
}
}
buildMenu($menu);
So how it needs to go:
Home
Parent - selected
Sub 1 - selected
Sub 2
or
Home
Parent - selected
Sub 1
Sub 2 - selected
Hope someone understand what i want? If my children under parent is selected also parent needs to be selected.
I have added one function to check element in children array. May be something better solution are there. But at this its quick solution for you :)
$menu = array(
array(
'title' => 'Home',
'link' => 'a'
),
array(
'title' => 'Parent',
'link' => 'b',
'children' => array(
array(
'title' => 'Sub 1',
'link' => 'c'
),
array(
'title' => 'Sub 2',
'link' => 'd'
),
)
)
);
function buildMenu($menuArray) {
foreach ($menuArray as $node) {
$getMenu = isset($_GET['menu']) ? $_GET['menu'] : '';
$checkParent = (isset($node['children']) && !empty($node['children'])) ? checkInChildArray($getMenu, $node['children']) : '';
$parentSelected = ($checkParent) ? $selected = 'style="color: red;"' : null;
echo "<li " . $parentSelected . "><a href='?menu=" . $node['link'] . "'>" . $node['title'] . "</a></li>";
if (isset($node['children']) && !empty($node['children'])) {
echo "<ul>";
foreach ($node['children'] as $subMenu) {
$childSelected = ($subMenu['link'] == $getMenu) ? $selected = 'style="color: red;"' : null;
echo "<li " . $childSelected . "><a href='?menu=" . $subMenu['link'] . "'>" . $subMenu['title'] . "</a></li>";
}
echo "</ul>";
}
echo "</li>";
}
}
// Checking if selected menu inside children array.
function checkInChildArray($needle, $haystack, $strict = false) {
foreach ($haystack as $item) {
if (($strict ? $item['link'] === $needle : $item == $needle) || (is_array($item) && checkInChildArray($needle, $item, $strict))) {
return true;
}
}
return false;
}
echo buildMenu($menu);
Working Demo
Use jQuery to add parent li a background color
$('li.selected').parent().closest('li').css("color","red");
Pass every menu level to the url. You can add the "selected" class for every menu level. So:
$current_menu_level_1 = (isset($_GET['menu_level_1'])) ? $_GET['menu_level_1'] : false;
$current_menu_level_2 = (isset($_GET['menu_level_2'])) ? $_GET['menu_level_2'] : false;
When building your menu, compare the 'to be build item' to the $current_menu_level1/2 variable and add echo the class when they are the same.
please, consider create a css class selected for <li> elements and <ul> elements.
With PHP insert this styles when needed, Like this:
$selected = ($node['link']== $_GET['menu']) ? $selected = 'selected' : '';
echo "<li class='".$selected."'>";
echo "<ul class='".$selected."'>";
buildMenu($node['children']);
echo "</ul>";

php create navigation menu from multidimensional array dynamically

I did research on this, and wasn't able to find an exact answer. Most of the questions/answers on here pertaining to this seem to be unfinished. If anyone knows of a finished solution similar to my question, please point me in that direction!
Here is my array:
Array
(
['home'] => Array
(
[0] => sub-home1
[1] => sub-home2
)
['about'] => Array
(
[0] => sub-about
['about2'] => Array
(
[0] => sub-sub-about
)
)
['staff'] => Array
(
[0] => sub-staff1
[1] => sub-staff2
)
['contact'] => contact
)
And here is what I would like to turn it into:
<ul>
<li><a href="">home<a/>
<ul>
<li>sub-home1</li>
<li>sub-home2</li>
</ul>
</li>
<li><a href="">about<a/>
<ul>
<li>sub-about</li>
<li>about2
<ul>
<li><a href="">sub-sub-about<a/></li>
</ul>
</li>
</ul>
</li>
<li><a href="">staff<a/>
<ul>
<li>sub-staff1</li>
<li>sub-staff2</li>
</ul>
</li>
<li><a href="">contact<a/></li>
</ul>
The array will be dynamically generated, but will have a limit of 3 levels ex: about->about2->sub-sub-about. I tried going off of this question: PHP/MySQL Navigation Menu but they didn't really seem to come to a conclusion? I am familiar with foreach's whiles and for loops but I just can't seem to wrap my head around this one.
EDIT: Enzino, your code works!
Here is my solution:
<?php
function MakeMenu($items, $level = 0) {
$ret = "";
$indent = str_repeat(" ", $level * 2);
$ret .= sprintf("%s<ul>\n", $indent);
$indent = str_repeat(" ", ++$level * 2);
foreach ($items as $item => $subitems) {
if (!is_numeric($item)) {
$ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $item);
}
if (is_array($subitems)) {
$ret .= "\n";
$ret .= MakeMenu($subitems, $level + 1);
$ret .= $indent;
} else if (strcmp($item, $subitems)){
$ret .= sprintf("%s<li><a href=''>%s</a>", $indent, $subitems);
}
$ret .= sprintf("</li>\n", $indent);
}
$indent = str_repeat(" ", --$level * 2);
$ret .= sprintf("%s</ul>\n", $indent);
return($ret);
}
$menu = Array(
'home' => Array("sub-home1", "sub-home2"),
'about' => Array("sub-about", "about2" => Array("sub-sub-about")),
'staff' => Array("sub-staff1", "sub-staff2"),
'contact' => "contact"
);
print_r($menu);
echo MakeMenu($menu);
?>
Calvin's solution worked for me. Here's the edited version. We can use more nested loops to get sub - sub menu items.
echo '<ul>';
foreach ($menu as $parent) {
echo '<li>' . $parent . '';
if (is_array($parent)) {
echo '<ul>';
foreach ($parent as $children) {
echo '<li>' . $children . '';
}
echo '</ul>';
}
echo '</li&gt';
}
echo '</ul>';
I think you can use recursion? Here is some pseudocode, not very familiar with php.
function toNavMenu(array A){
for each element in A{
echo "<li>" + element.name + ""
if (element is an array){
echo "<ul>"
toNavMenu(element)
echo "</ul>"
}
echo "</li>"
}
}
I would probably slightly adapt the array to be something like the following:
Array(
0 => Array(
'title' => 'Home',
'children' => Array()
),
1 => Array(
'title' => 'Parent',
'children' => Array(
0 => Array(
'title' => 'Sub 1',
'children' => Array(),
),
1 => Array(
'title' => 'Sub 2',
'children' => Array(
0 => Array(
'title' => 'Sub sub 2-1',
'children' => Array(),
),
),
),
)
)
)
With a structure like this you could use recursion to build your menu HTML:
function buildMenu($menuArray)
{
foreach ($menuArray as $node)
{
echo "<li><a href='#'/>" . $node['title'] . "</a>";
if ( ! empty($node['children'])) {
echo "<ul>";
buildMenu($node['children']);
echo "</ul>";
}
echo "</li>";
}
}

PHP Recursive array looping and formatting

I have some PHP code I've been working on for a good few days now. I'm trying to generate a formatted list of rules from a flat array. I got help here before on how to turn the flat array into a tree array, but I'm having difficulty writing a recursive function that can go through it and successfully break it down at points at such depths where I'd like the rules to be members of an unordered list from the markup that gets printed.
<?php
$data = array(
'0' => 'Introduction',
'4' => 'General',
'4.1' => 'Chat',
'4.1.1' => 'Do',
'4.1.1.9' => 'This',
'4.1.1.10' => 'That',
'4.1.1.11' => 'Other',
);
$struct = array(
'children' => array()
);
foreach ($data as $ruleID => $content)
{
$parent =& $struct;
foreach (explode('.', $ruleID) as $val)
{
if (!isset($parent['children'][$val]))
{
$parent['children'][$val] = array(
'content' => '',
'children' => array()
);
}
$parent =& $parent['children'][$val];
}
$parent['content'] = $content;
}
$out = '';
$rules = array_pop($struct);
format_rule($rules);
var_dump($rules);
echo $out;
function format_rule($arr, $depth=0)
{
global $out;
echo "depth: $depth\n";
foreach($arr as $key => $val)
{
switch($depth)
{
case 0:
$out .= '<h1>'.$val['content']."</h1><br />\n";
break;
case 1:
$out .= '<h2>'.$val['content']."</h2><br />\n";
break;
case 2:
$out .= '<h3>'.$val['content']."</h3><br />\n";
break;
default:
$out .= '<li>'.$val['content']."</li>\n";
break;
}
if(isset($val['children']) && count($val['children']) > 0)
{
if($depth > 2)
{
$out .= '<ul>';
format_rule($val['children'], ++$depth);
$out .= '</ul>';
}
else
{
format_rule($val['children'], ++$depth);
}
}
}
}
The output at the moment is:
<h1>Introduction</h1><br />
<h1>General</h1><br />
<h2>Chat</h2><br />
<h3>Do</h3><br />
<li>This</li><br />
<li>That</li><br />
<li>Other</li><br />
Which is great, except from my code I'm pretty sure the section under 'Do' should have a <ul> around it.
change your code to :
if($depth >= 2)
note: remember the count starts at 0, not 1.
Try this:
<?php
$data = array(
'0' => 'Introduction',
'4' => 'General',
'4.1' => 'Chat',
'4.1.1' => 'Do',
'4.1.1.9' => 'This',
'4.1.1.10' => 'That',
'4.1.1.11' => 'Other',
);
function get_level($key){
return count(explode(".",$key));
}
function set_tag(&$array,$key,$item,&$ul_started){
$level = get_level($key);
switch($level){
case 1:
case 2:
case 3:
if($ul_started){
$array[$key] = "</ul><h".$level.">".$item."</h".$level."><br>";
$ul_started=false;
}else{
$array[$key] = "<h".$level.">".$item."</h".$level."><br>";
}
break;
default:
if(!$ul_started){
$array[$key] = "<ul><li><strong>".$item."</strong></li><br>";
$ul_started=true;
}else{
$array[$key] = "<li><strong>".$item."</strong></li><br>";
}
break;
}
}
$ul_started = false;
foreach($data as $key=>$item){
set_tag($data,$key,$item,$ul_started);
}
if($ul_started){
$keys = array_keys($data);
$data[$keys[count($data)-1]] .= "</ul>";
}
echo implode("",$data);
?>

Categories