我有一个包含一堆类别的数据库,有些是孩子:
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
这是我的最终解决方案,重用数组而不是执行数据库查询:
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;
}