我有一堆類別的數據庫,有些孩子:創建一個遞歸目錄樹功能
Array
(
[0] => Array
(
[id] => 1
[name] => Home Improvement
[slug] => Home-Improvement
[parent] =>
[user_id] => 1
[order] => 1
)
[1] => Array
(
[id] => 2
[name] => Asbestos Abatement & Removal
[slug] => Asbestos-Abatement-Removal
[parent] => 1
[user_id] => 1
[order] => 8
)
[2] => Array
(
[id] => 3
[name] => Asphalt & Asphalt Products
[slug] => Asphalt-Asphalt-Products
[parent] => 1
[user_id] => 1
[order] => 9
)
[3] => Array
(
[id] => 4
[name] => Bathroom
[slug] => Bathroom
[parent] => 1
[user_id] => 1
[order] => 10
)
[4] => Array
(
[id] => 5
[name] => Kitchen Cabinets
[slug] => Kitchen-Cabinets
[parent] => 1
[user_id] => 1
[order] => 11
)
[5] => Array
(
[id] => 6
[name] => Ceilings
[slug] => Ceilings
[parent] => 1
[user_id] => 1
[order] => 12
)
[6] => Array
(
[id] => 7
[name] => Cleaning
[slug] => Cleaning
[parent] => 1
[user_id] => 1
[order] => 13
)
[7] => Array
(
[id] => 8
[name] => Closet Organizers & Accessories
[slug] => Closet-Organizers-Accessories
[parent] => 1
[user_id] => 1
[order] => 14
)
[8] => Array
(
[id] => 9
[name] => Concrete
[slug] => Concrete
[parent] => 1
[user_id] => 1
[order] => 15
)
[9] => Array
(
[id] => 10
[name] => Contractors & Service Providers
[slug] => Contractors-Service-Providers
[parent] => 1
[user_id] => 1
[order] => 16
)
我試圖輸出是這樣的:
<ul>
<li>Parent
<ul>
<li>Child</li>
</ul>
</li>
<li>Parent with no Children</li>
</ul>
我試圖在PHP中構建遞歸樹腳本,但我被卡住了。這是迄今爲止我所擁有的。我堅持要在else和endif之間做些什麼。在foreach。 (我使用這種語法只是爲了方便閱讀。)任何建議?
echo $this->categories->makeTree(0, $this->db->get('categories')->result_array());
public static function makeTree($parent, $array)
{
if (!is_array($array)) return '';
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
else:
endif;
endif;
$output .= '</li>';
endforeach;
$output .= '</ul>';
return $output;
}
編輯1
我能得到這個工作,雖然我有一個foreach循環,這可能不是最好的主意,一個數據庫調用:
public function makeTree($parent, $array)
{
if (!is_array($array)) return FALSE;
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
$subcategories = ci()->db->get_where('categories', array('parent' => $value['id']));
if ($subcategories->num_rows() > 0):
$output .= $this->makeTree($value['id'], $subcategories->result_array());
endif;
else:
$output .= $value['name'];
$output .= '</li>';
endif;
endif;
endforeach;
$output .= '</ul>';
return $output;
}
編輯2
這是我的最終解決方案,重用數組,而不是做一個DB曲線紅黴素:
public function makeTree($parent, $array)
{
if (!is_array($array) OR empty($array)) return FALSE;
$output = '<ul>';
foreach($array as $key => $value):
if ($value['parent'] == $parent):
$output .= '<li>';
if ($value['parent'] == NULL):
$output .= $value['name'];
$matches = array();
foreach($array as $subkey => $subvalue):
if ($subvalue['parent'] == $value['id']):
$matches[$subkey] = $subvalue;
endif;
endforeach;
$output .= $this->makeTree($value['id'], $matches);
else:
$output .= $value['name'];
$output .= '</li>';
endif;
endif;
endforeach;
$output .= '</ul>';
return $output;
}
它目前遍歷2個級別。我們可以將它遍歷到n級嗎? –