如何从数组创建菜单

时间:2015-11-26 04:05:33

标签: php recursion multidimensional-array menu

我有一个像这样的数据库数组。我想将数组放入这样的菜单中。

  • 设置
    • 菜单类别
      • 菜单类别视图
      • 群组视图
    • 矿床
    • 组菜单
      • 群组菜单视图
    • 菜单
      • 菜单视图
  • ETC

我一直在尝试但到目前为止,每次尝试都失败了

array(
(int) 0 => array(
    'MenuCategory' => array(
        'name' => 'Setting'
    ),
    'Menu' => array(
        'name' => 'Menu Category',
        'url' => '/menu_categories/adding',
        'parent_id' => null
    ),
    'Site' => array(
        'id' => (int) 1
    ),
    'Sub' => array(
        'name' => 'Menu Category View',
        'url' => '/menu_categories/view'
    )
),
(int) 1 => array(
    'MenuCategory' => array(
        'name' => 'Setting'
    ),
    'Menu' => array(
        'name' => 'Group',
        'url' => '/groups/adding',
        'parent_id' => null
    ),
    'Site' => array(
        'id' => (int) 1
    ),
    'Sub' => array(
        'name' => 'Group View',
        'url' => '/groups/view'
    )
),
(int) 2 => array(
    'MenuCategory' => array(
        'name' => 'Setting'
    ),
    'Menu' => array(
        'name' => 'Deposits',
        'url' => '/Deposits/requestTiket',
        'parent_id' => null
    ),
    'Site' => array(
        'id' => (int) 1
    ),
    'Sub' => array(
        'name' => null,
        'url' => null
    )
),
(int) 3 => array(
    'MenuCategory' => array(
        'name' => 'Setting'
    ),
    'Menu' => array(
        'name' => 'Group Menus',
        'url' => '/group_menus/adding',
        'parent_id' => null
    ),
    'Site' => array(
        'id' => (int) 1
    ),
    'Sub' => array(
        'name' => 'Group View',
        'url' => '/group_menus/view'
    )
),
(int) 4 => array(
    'MenuCategory' => array(
        'name' => 'Setting'
    ),
    'Menu' => array(
        'name' => 'Menu',
        'url' => '/menus/adding',
        'parent_id' => null
    ),
    'Site' => array(
        'id' => (int) 1
    ),
    'Sub' => array(
        'name' => 'Menu View',
        'url' => '/menus/view'
    )
)

);

谁能帮助我吗? ?

2 个答案:

答案 0 :(得分:0)

您可以使用三个嵌套的foreach()循环来循环遍历它,如下所示:

$arr = // your array;
echo "<ul>";
foreach($arr as $setting => $nested) {
    echo "<li>$setting<ul>";
    foreach($nested as $category => $nested2) {
        echo "<li>$category</li><ul>";
        foreach($nested2 as $view) {
            echo "<li>$view</li>";
        }
        echo "</ul></li>";
    }
    echo "</ul></li>";
}
echo "</ul>";

唯一的问题是它可能打印出不必要的(空)<ul>,并且<ul>默认为不同的子弹类型(虽然很容易用CSS修复)。

PHP SandboxIdeone进行测试。这是提供的输出:

<ul><li>setting<ul><li>menu category</li><ul><li>menu category view</li></ul></li><li>group</li><ul><li>group view</li></ul></li><li>deposits</li><ul></ul></li><li>group menu</li><ul><li>group menu array</li></ul></li><li>menu</li><ul><li>menu view</li></ul></li></ul></li></ul>

答案 1 :(得分:0)

        import java.util.Scanner;

        public class ARRAY123 {

        static int Yaki2=0;
        static String ff, ll;
        static Scanner Yaki = new Scanner (System.in);
        public static void main (String[]a){

//String [] Names = new Array (5);
        char menuInput;
        char retry;
        String [] names;
        names = new String [5];

        names [0] = "";
        names [1] = "";
        names [2] = "";
        names [3] = "";     
        names [4] = "";


        do {
        System.out.println("Names in the Array");
        System.out.println("Choose what you want to do with the Array:");
        System.out.println("[A]ADD");
        System.out.println("[B]EDIT");
        System.out.println("[C]DELETE");
        System.out.println("[D]SORT");
        System.out.println("[E]SEARCH");

        menuInput = Yaki.next().charAt(0);
        switch (menuInput) {
        case 'a':
        case 'A':
        System.out.println("Add Elements to the Array");
        System.out.print("Input whatever element you want to add:");
        add(names);
        break;
        case 'b':
        case 'B':
        System.out.println("Edit an Element in the Array");
        ll = Yaki.next();
        System.out.println("Palitan mo na");
        ff = Yaki.next();
        wait 
        (ll,ff);
        break;                

        }       
        System.out.println("Would you like to do more?");
        retry = Yaki.next().charAt(0);
        }while (retry == 'Y'|| retry == 'y');

        System.out.println(names[0]);
        System.out.println(names[1]);
        System.out.println(names[2]);
        System.out.println(names[3]);
        System.out.println(names[4]);
        }

        private static void wait(String ll2, String ff2) {
            // TODO Auto-generated method stub

        }

        static void add (String a[]) {


        if (Yaki2 < a.length) {
        a[Yaki2] = Yaki.next();
        Yaki2++;}
        else 
        System.out.println("The Elements in the Array are complete, you can no longer add more");

        }

        }