Output HTML Tree With Array - php

Is there a more elegant way to output the given html structure with an array? I found some recursive solutions but therefore a parent_id is needed (if I understand correctly).
HTML
<ul>
<li>Level 1</li>
<li>
<ul>
<li>Level 2</li>
<li>
<ul>
<li>Level 3</li>
</ul>
</li>
</ul>
</li>
</ul>
Array
Array
(
[0] => Level 1
[1] => Array
(
[0] => Level 2
[1] => Array
(
[0] => Level 3
)
)
)
Loops
<ul>
<?php foreach($array as $arr) : ?>
<li>
<?php if(is_array($arr)) : ?>
<ul>
<?php foreach($arr as $a) : ?>
<li>
<?php if(is_array($a)) : ?>
<ul>
<?php foreach($a as $aa) : ?>
<li><?php echo $aa; ?></li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<?php echo $a; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else : ?>
<?php echo $arr; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
I´m a bit worried about the performance of this approach :-D

do it just recursive:
<?PHP
function doOutputList($TreeArray)
{
echo '<ul>';
foreach($TreeArray as $arr)
{
echo '<li>';
if(is_array($arr))
{
doOutputList($arr);
}
else
{
echo $arr;
}
echo '</li>';
}
echo '</ul>';
}
doOutputList($array);
?>
Or if you like good readable HTML Code try this:
<?PHP
function doOutputList($TreeArray, $deep=0)
{
$padding = str_repeat(' ', $deep*3);
echo $padding . "<ul>\n";
foreach($TreeArray as $arr)
{
echo $padding . " <li>\n";
if(is_array($arr))
{
doOutputList($arr, $deep+1);
}
else
{
echo $padding .' '. $arr;
}
echo $padding . " </li>\n";
}
echo $padding . "</ul>\n";
}
doOutputList($array);
?>

Related

foreach every 3 put inside a new ul

Actually I have this foreach loop:
<ul>
<?php foreach ( $tasks as $task ) { ?>
<li>
...
</li>
<?php } ?>
</ul>
It returns:
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
<li>...</li>
...
</ul>
Now I need to change the loop to put every 3 <li> into a new <ul>, so I can have:
<div>
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
...
</div>
How I have to modify the foreach loop to achieve that result?
This should work for you:
Just array_chunk() your array into chunks of 3 and then implode() each subArray, e.g.
<div>
<?php
foreach (array_chunk($tasks, 3) as $task) {
echo "<ul><li>" . implode("</li><li>", $task) . "</li></ul>";
}
?>
</div>
try this
foreach (array_chunk($arrays, 3, true) as $array)
{
echo '<ul>';
foreach($array as $value)
{
echo "<li>".$value."</li>";
}
echo '</ul>';
}
Please try this..
<ul>
<?php foreach ( $tasks as $k=>$task ) { ?>
<?php if($k % 3 == 1) { ?>
<ul>
<?php } ?>
<li></li>
<?php if($k % 3 == 0) { ?>
</ul>
<?php } ?>
<?php } ?>
</ul>
I like #Rizer123's answer for conciceness, but if you wish to keep iterating over the individual items, rather than chunking (for example; if you need to perform some manipulation on the individual items) I'd suggest something like the following:
<ul>
<?php foreach ( array_values($tasks) as $k => $task ) {
if($k % 3 == 0) { ?>
<ul>
<?php } ?>
<li>
...
</li>
if($k % 3 == 2) { ?>
</ul>
<?php } ?>
<?php } ?>
</ul>

Nested ul li with a php array and a specific count

I have 10 users in an array:
$array = array(
"aaa",
"bbb",
"ccc",
"ddd",
"eee",
"fff",
"ggg",
"hhh",
"iii",
"jjj",
);
And I want to display lists based on 5 or fewer users e.g.:
<ul>
<li>
<ul>
<li>aaa</li>
<li>bbb</li>
<li>ccc</li>
<li>ddd</li>
<li>eee</li>
</ul>
</li>
<li>
<ul>
<li>fff</li>
<li>ggg</li>
<li>hhh</li>
<li>iii</li>
<li>jjj</li>
</ul>
</li>
</ul>
At the moment I have:
<ul>
<?php foreach($users as $user): ?>
<li><?php echo $user ?></li>
<?php endforeach; ?>
</ul>
However I am not creating the inner uls. What is the best way to approach this? Using a for loop and counting out 5? Or is there a neater method?
Use array_chunk() to split an array into multiple arrays with a specified number of items.
<ul>
<?php
$users = array_chunk($array, 5);
foreach ($users as $user) {
echo "<li><ul>";
foreach ($user as $idv) {
echo "<li>" . $idv . "</li>";
}
echo "</ul></li>";
}
?>
</ul>
Look an alternative solution :)
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,0,5)).'</li>';
?>
</ul>
</li>
<li>
<ul>
<?php
echo '<li>'.join('</li><li>',array_slice($array,5,10)).'</li>';
?>
</ul>
</li>
<ul>
<?php
$lenght = count($users);
for( $i=0; $i<$lenght; $i++ ){
if( $i%5 == 0 ){
echo '<li><ul>';
}
echo '<li>'.$users[0].'</li>';
if( $i%5 == 4 || ($i+1==$lenght) ){
echo '</ul></li>';
}
}?>
</ul>

Tree Structure using ul,li format in php

Code for Tree structure using ul, li in php...
*In the output i am missing ul or in some place.*
I want this for checkboxes when we click the parent check box their childs must be selected and sub childs too.
<?php
$groups = array(
array('depth'=>0,'value'=>'DEF'),
array('depth'=>1,'value'=>'DEF 1'),
array('depth'=>2,'value'=>'DEF_1'),
array('depth'=>1,'value'=>'DEF2'),
array('depth'=>0,'value'=>'GROUP'),
array('depth'=>1,'value'=>'GROUP1'),
array('depth'=>1,'value'=>'GROUP2'),
array('depth'=>2,'value'=>'GROUP2_1'),
array('depth'=>2,'value'=>'TELUGU'),
array('depth'=>3,'value'=>'RAM'),
array('depth'=>0,'value'=>'GROUP3'),
array('depth'=>1,'value'=>'GROUP3_1'),
array('depth'=>1,'value'=>'GROUP3_2'),
array('depth'=>1,'value'=>'GROUP3_3'),
array('depth'=>1,'value'=>'DEF2_1'),
array('depth'=>0,'value'=>'GROUP4'),
array('depth'=>0,'value'=>'T8')
);
$count = count($groups);
$old_depth = 0;
echo '<ul>'.chr(13).chr(10);
for($i = 0; $i<$count; $i++)
{
if($old_depth==$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if($old_depth!=$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
}
}
}
if($old_depth<$groups[$i]['depth'])
{
echo '<li>';
echo $groups[$i]['value'];
if(isset($groups[$i+1]))
{
if(($groups[$i]['depth'])+1==$groups[$i+1]['depth']){
echo '<ul>';
}
else
{
echo '</li>';
if(!($groups[$i]['depth']==$groups[$i+1]['depth']))
{
echo '</ul></li>';
}
if($groups[$i+1]['depth']==0)
{
echo '</ul></li>';
}
}
}
else
{
echo '</ul>';
}
}
}
echo '</li></ul>';
?>
I want the output in this format.
<ul>
<li>DEF
<ul>
<li>DEF1
<ul>
<li>DEF_1</li>
</ul>
</li>
<li>DEF2</li>
</ul>
</li>
<li>GROUP
<ul>
<li>GROUP1</li>
<li>GROUP2
<ul>
<li>GROUP2_1</li>
<li>TELUGU
<ul>
<li>RAM</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li>GROUP3
<ul>
<li>GROUP3_1</li>
<li>GROUP3_2</li>
<li>GROUP3_3</li>
<li>DEF2_1</li>
</ul>
</li>
<li>GROUP4</li>
<li>T8</li>
</ul>

Two elements print in one foreach loop than next loop start

I have an array
$foo = array(1,2,3,4,5,6,7,8,9);
when I use foreach loop for this
foreach($foo as $val):
print '<li>'.$val.'</li>';
endforeach
Out put is ,
<li> 1 </li>
<li> 2 </li>
<li> 3 </li>
<li> 4 </li>
<li> 5 </li>
But I want out put something like that
<li> 1, 2 </li>
<li> 3, 4 </li>
<li> 5, 6 </li>
<li> 7, 8 </li>
Is it possible?
$foo = array(1,2,3,4,5,6,7,8,9);
foreach (array_chunk($foo, 2) as $chunk) {
echo "<li>" . implode(', ', $chunk) . "</li>\n";
}
For php you can do:
for($i=0;$i<count($foo);$i+=2) {
echo "<li>{$foo[$i]}, {$foo[$i+1]}</li>";
}
This might be what you require:
foreach($foo as $key=>$val) {
if ($val&1) {
echo '<li>' . $val;
if($key == (count($foo)-1)){
echo '</li>';
}
} else {
echo ',' . $val . '</li>';
}
}
Please try this:
$foo = array(1,2,3,4,5,6,7,8,9);
$i=1;
$firstElement = "";
foreach($foo as $val):
if($i%2==0)
{
print '<li>'.$firstElement.','.$val.'</li>';
}
else
{
$firstElement = $val;
}
$i++;
endforeach

How do I add different class for my ordered lists

I want add left, center & right class to my ordered lists while loop.
Code
<?php while ($fetch) { ?>
<li>haha</li>
<?php } ?>
Results should be
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>
Let me know
<?php $classes = array("left","center","right");
$i = 0;
while ($fetch) {
?>
<li class="<?php echo $classes[$i++ % 3] ?>">haha</li>
<?php } ?>
$cnt=0;
while ($fetch)
{
switch ($cnt%3)
{
case 0 : $class = 'left'; break;
case 1 : $class = 'center'; break;
case 2 : $class = 'right'; break;
}
echo '<li class="', $class, '">haha</li>';
++$cnt;
}
I've just tested the following code and verified that it produces the desired output:
<?php
$items = array('haha', 'haha', 'haha', 'haha', 'haha', 'haha');
$cssClasses = array('left', 'center', 'right');
echo "<ul>\n";
$i=0;
foreach ($items as $item) {
echo "\t<li class=\"" . $cssClasses[$i++ % 3] . '">' . $item . "</li>\n";
}
echo "</ul>\n";
?>
The output is:
<ul>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
<li class="left">haha</li>
<li class="center">haha</li>
<li class="right">haha</li>
</ul>

Categories