I want to make parent to be new "ul" and child "li" item,but obviously parent will have to be "li" and "ul" too at the same time,bcz i want to build "parent child relationship list" with tree principle, child will be a parent of another element too and that will create another subcategory and etc... so how would you have done it correctly? if you couldn't understand something... ask. In image i just display how it should look like,simple tree
<?php
$tasks[] = array("id" => 1, "parent_id" => 0, "title" => 'task 1');
$tasks[] = array("id" => 2, "parent_id" => 1, "title" => 'sub task 1');
$tasks[] = array("id" => 3, "parent_id" => 1, "title" => 'sub task 2');
$tasks[] = array("id" => 4, "parent_id" => 2, "title" => 'sub sub task 1');
$tasks[] = array("id" => 5, "parent_id" => 2, "title" => 'task 2');
$tasks[] = array("id" => 6, "parent_id" => 2, "title" => 'sub task 3');
$branch = array();
function buildTree(array &$elements, $parentId = 0) {
$branch = array();
foreach ($elements as &$element) {
if ($element['parent_id'] == $parentId) {
$children = buildTree($elements, $element['id']);
if ($children) {
$element['children'] = $children;
// $element['parent']=$element['title'];
print_r($element['id']);
foreach($element['children'] as $child){
if($element['id']==$child['parent_id']){
echo '<ul>';
echo '<li>'.$child['title'].'</li>';
echo '</ul>';
}
}
}
$branch[$element['id']] = $element;
}
}
return $branch;
}
print_r(buildTree($tasks));
?>