我有一个函数,它工作得很好。让我问一下,是否有可能保存功能但删除递归:
public static function GenerateNavHTML($items, $tabs = '') {
$html = !strlen($tabs) ?
$tabs.'<ul class="dd-list">' :
$tabs.'<ul>';
foreach($items as $item) {
$item = (object)$item;
$html .= $tabs." ".'<li class="dd-item" data-id="'.(int)$item->id.'">';
$html .= '<div class="dd-handle">Drag</div>';
$html .= '<div class="dd-content">
<span class="title">'.(string)$item->name.'</span>
<div id="action" class="btn-group pull-right">
<button type="button" class="btn btn-primary btn-xs" title="Edit category" data-action="edit" data-id="'.(int)$item->id.'" data-category="'.(string)$item->name.'"><i class="fa fa-edit"></i></button>
<button type="button" class="btn btn-warning btn-xs" title="Delete category and all nested categories" data-action="delete" data-id="'.(int)$item->id.'" data-category="'.(string)$item->name.'"><i class="fa fa-close"></i></button>
</div>
</div>';
if(isset($item->children[0])) {
$html .= self::GenerateNavHTML($item->children, $tabs." ");
}
$html .= '</li>';
}
$html .= $tabs.'</ul>';
return $html;
}发布于 2015-12-01 05:50:09
您可以考虑使用Queue来实现这一点。如果我没弄错的话,你的$item可以有多个孩子,每个孩子都需要类似的处理(for循环等)。您可以使用SplQueue并在找到每个项目后立即将其放入队列中。下面是psuedo代码:
$itemQ = queue of all top level items
foreach $item in $itemQ
//do processing here
if $item->children
$itemQ.add( $item->children)https://stackoverflow.com/questions/34008492
复制相似问题