0

现在我有一个具有 parent_id 的数组 & 我想我需要使用递归来完成我想做的事情

array
  0 => 
    object(Menu)[56]
      public 'attributes' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  1 => 
    object(Menu)[60]
      public 'attributes' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  2 => 
    object(Menu)[61]
      public 'attributes' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty

array
  0 => 
    object(Menu)[56]
      public 'attributes' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  1 => 
    object(Menu)[60]
      public 'attributes' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  2 => 
    object(Menu)[61]
      public 'attributes' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty

array
  0 => 
    object(Menu)[56]
      public 'attributes' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  1 => 
    object(Menu)[60]
      public 'attributes' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  2 => 
    object(Menu)[61]
      public 'attributes' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty

array
  0 => 
    object(Menu)[56]
      public 'attributes' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '1' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'home' (length=4)
          'menu_url' => string 'test' (length=4)
          'parent_id' => string '0' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  1 => 
    object(Menu)[60]
      public 'attributes' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '2' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Contact Us' (length=10)
          'menu_url' => string 'contact' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty
  2 => 
    object(Menu)[61]
      public 'attributes' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'original' => 
        array
          'id' => string '3' (length=1)
          'menu_id' => string '1' (length=1)
          'menu_name' => string 'Privacy Docs' (length=12)
          'menu_url' => string 'privacy' (length=7)
          'parent_id' => string '1' (length=1)
          'position' => string '0' (length=1)
          'group_id' => string '1' (length=1)
          'menu_items' => null
      public 'relationships' => 
        array
          empty
      public 'exists' => boolean true
      public 'includes' => 
        array
          empty

现在我在一个名为 menuHelper 的类中有一个函数

public static function make(array $array, $no = 0) {
        var_dump($array);
        $child = self::hasChildren($array, $no);
        if (empty($child))
            return "";
        $content = "<li>\n";
        foreach ($child as $value) {
            $content = sprintf("\t<li><div>%s</div>" . self::make($array, $value->id) . "</li>\n", $value->menu_name);
            //$content .= self::make($array, $value->id);
        }
        $content .= "</li>\n";
        //return $content;
        return($content == '' ? '' : "<ol>" . $content . "</ol>");
    }
public static function hasChildren($array, $id) {
        return array_filter($array, function ($var) use($id) {
                    return $var->parent_id == $id;
                });
    }

我希望这个输出这个

<ol class="sortable">
    <li>
        <div>parent</div>
        <ol>
            <div>Sub</div>
            <ol>
                <div>Sub 2</div>
            </ol>
        </ol>
    </li>
    <li>
        <div>parent 2</div>
        <ol>
            <div>Sub</div>
            <ol>
                <div>Sub 2</div>
            </ol>
        </ol>
    </li>
</ol>

但这并没有继续输出父母,而是输出 1 个父母,然后是它的孩子。然后即使有另一个父母它也会退出输出所以我想我有什么问题吗?

4

1 回答 1

0

这是一个非常粗略的答案,因为您的问题含糊不清且缺少细节。

您需要将主菜单项存储在一个数组中,然后遍历它们以绘制主菜单。然后,当您遍历每个主菜单项时,查找/加载父级可能拥有的任何子菜单项。如果有子菜单项 - 绘制子菜单。十分简单。

<ol class="sortable">
<?php
$main_menu_items = array( $first_menu_item, $second_menu_item );
foreach($main_menu_items as $menu_item)
{
    ?>
    <li>
    <div><?=$menu_item['menu_name']?></div>
    <?php
    $child_menu_items = get_child_menu_items($menu_item['menu_id']);
    if( count($child_menu_items) > 0 )
    {
        ?>
        <ol>
        <?php
        foreach($child_menu_items as $child_item)
        {
            ?>
            <li><div><?=$menu_item['menu_name']?></div></li>
            <?php
        }
        ?>
        </ol>
        <?php
    }
    ?>
    </li>
    <?php
}
?>
</ol>
于 2013-04-08T05:19:44.703 回答