I have this code:
foreach($list as $id => $item_array) {
if (($item_array['approved'] == 1) || $is_admin) {
include('item.php');
}
$temp = $item_array;
while(!empty($temp['child'])) {
foreach($temp['child'] as $id => $item_array) {
if (($item_array['approved'] == 1) || $is_admin) {
include('item.php');
}
}
if(empty($temp['child']['child'])) {
unset($temp);
}
}
}
which functions but I'm going to need to duplicate the while
block 5 times because my item
s can have up to six levels. There must be a more efficient way to do this, right? (The item.php
just outputs HTML based on the array values)
In the simplest form $list
would be:
Array
(
[id] => 1
[name] => Water
[date] => 2018-05-14 13:24:22
[content] => It's Good
[approved] => 1
[level] => 0
[child] => Array([0] => Array(
[id] => 2
[name] => Hydrogen
[date] => 2017-05-07 15:12:14
[content] => Part air
[approved] => 1
[level] => 1)
)
)
If needed I build $list
with:
if($parent != -1) {
$list[$parent]['child'][] = $this->build_list_array($id, $date, $content, $name, $approved, $level);
} else {
$list[$id] = $this->build_list_array($id, $date, $content, $name, $approved, $level);
}
function build_comment_array($id, $date, $content, $name, $approved, $level) {
return array('id' => $id,
'name' => $name,
'date' => $date,
'content' => $content,
'approved' => $approved,
'level' => $level);
}