PHP:从数组生成类别/子类别UL列表?

I have an array filled with "Category" objects, each one containing an id and a parent_id value. They're accessed like this:

$category->get("id"); //gets category id
$category->get("parent_id"); gets parent category id

I want to make a HTML UL list like this:

<ul>
    <li>Category</li>
    <li>Category
        <ul>
            <li>Child Category</li>
            <li>Child Category</li>
            <li>Child Category
                <ul>
                    <li>Child Category</li>
                    <li>Child Category</li>
                    <li>Child Category</li>
                </ul>
            </li>
        </ul>
    </li>
    <li>Category</li>
    <li>Category</li>
    <li>Category</li>
    <li>Category</li>
</ul>

What sort of loop must be done to generate this?

without knowing what library you are using, it will be more of a pseudo code than working code but you should get the idea of how to use recursion to get the tree

first retrieve the main category (you could set that for example as category 0 is the top category. then loop through all items and fetch children. If node has children, call itself recursively

showCategory($rootcategory) 
function showCategory($category) {
    $children=fetchChildren();
    if($children) //if category has children
    {
      echo('<ul>');
      foreach($children as $child) {
          showCategory($child);
      }
      echo('</ul>');
    }
    else {
      echo('<li>' . $child['title'] . '</li>');
    }
}