2015-07-20 56 views
1

我正在尋找一個函數,它需要一個頁面/類別(來自平面數據庫結果)的數組,並且回顯到基於嵌套頁面/類別<ul>(HTML無序列表)項目在父母的ID上。我想遞歸地做到這一點,以便可以完成任何級別的嵌套。PHP-Echo multidimentional array as HTML列表

+-------+---------------+---------------------------+ 
| id | parent_id |   title   | 
+-------+---------------+---------------------------+ 
| 1 |  0  | Parent Page    | 
| 2 |  1  | Sub Page    | 
| 3 |  2  | Sub Sub Page   | 
| 4 |  0  | Another Parent Page  | 
+-------+---------------+---------------------------+ 

這是我想與工藝,結束了在我看來,文件中的數組:

Array 
(
[0] => Array 
    (
     [id] => 1 
     [parent_id] => 0 
     [title] => Parent Page 
     [children] => Array 
        (
         [0] => Array 
          (
           [id] => 2 
           [parent_id] => 1 
           [title] => Sub Page 
           [children] => Array 
              (
               [0] => Array 
                (
                 [id] => 3 
                 [parent_id] => 1 
                 [title] => Sub Sub Page 
                ) 
              ) 
          ) 
        ) 
    ) 
[1] => Array 
    (
     [id] => 4 
     [parent_id] => 0 
     [title] => Another Parent Page 
    ) 
) 

我發現this文章是相似NOT DUPLICATE到一個我發佈,這是我發現下面的功能,可以幫助答案。

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; 
     } 
     $branch[] = $element; 
    } 
} 

return $branch; 
} 

$tree = buildTree($rows); 

編輯:

我想呼應的結構如下數據:

<ul> 
 
    <li><a href="#">Parent Page</a> 
 
    <ul> 
 
     <li><a href="#">Sub Page</a> 
 
     <ul> 
 
      <li><a href="#">Sub Sub Page</a> 
 
     </ul> 
 
     </li> 
 
    </ul> 
 
    </li> 
 
    <li><a href="#">Another Parent Page</a> 
 
    </li> 
 
</ul>

+0

做u想要什麼? –

+0

我已添加編輯以使其更清晰 – dzerow

回答

1

試試這個功能和變化根據您的要求:

function buildTree(Array $data, $parent = 0) { 
$tree = array(); 
foreach ($data as $d) { 
    if ($d['parent'] == $parent) { 
     $children = buildTree($data, $d['id']); 
     // set a trivial key 
     if (!empty($children)) { 
      $d['_children'] = $children; 
     } 
     $tree[] = $d; 
    } 
} 
return $tree; 
} 

function printTree($tree, $r = 0, $p = null) { 
foreach ($tree as $i => $t) { 
    $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) .' '; 
    printf("\t<option value='%d'>%s%s</option>\n", $t['id'], $dash, $t['name']); 
    if ($t['parent'] == $p) { 
     // reset $r 
     $r = 0; 
    } 
    if (isset($t['_children'])) { 
     printTree($t['_children'],$sel, ++$r, $t['parent']); 
    } 
} 
} 
$rows = array('your array'); 

$tree = buildTree($rows); 

print("<select name='selectionParentpage' class='just_textfield' id='selectionParentpage'><option value=''>--Select Page--</option>\n"); 
printTree($tree); 
print("</select>"); 
0

這裏是你可以在視圖部分做什麼

function displayRecursive($array){ 
    echo "<ul>"; 
     foreach($array as $value){ 
     echo "<li><a href='#'>".$value['name']."</a>"; 
      if(isset($value['children']) && !empty($value['children'])){ 
       displayRecursive($value['children']); 
      } 
     } 
    echo "</ul>"; 
} 

您可以使用像這樣得到的樹狀排列:

function recursive($parentId=0){ 

    $return = array(); 

    $titles=$this->db->query(" 
       SELECT  `t1`. * , `t2`.`id` AS `has_children` 
       FROM  `titles` `t1` 
       LEFT JOIN `titles` `t2` ON (`t1`.id = `t2`.`parent_id`) 
       WHERE  `t1`.`parent_id` = '$parentId' 
       GROUP BY `t1`.`id` ")->result_array(); 
    if(!empty($titles)){   
     foreach ($titles as $k=>$title){ 
      $item = $title; 
      if ($title['has_children']>0){ 
       $item['sub'] = self::recursive($title['id']); 
      } 
      array_push($return, $item); 
     } 
    } 

    return $return; 
}