dongxuxian1123 2015-04-28 22:07
浏览 57
已采纳

PHP-检查多维数组中的子数组

I have an array which contains the data to build a nav menu on the site.

This is how it's setup:

$menu = array();

$menu['0']['label'] = 'Home';
$menu['0']['icon'] = 'fa-home';
$menu['0']['id'] = '';
$menu['0']['class'] = '';
$menu['0']['url'] = '/index.php';
$menu['0']['blank'] = 0;

$menu['1']['label'] = 'Admin';
$menu['1']['icon'] = 'fa-user';
$menu['1']['id'] = '';
$menu['1']['class'] = '';
$menu['1']['url'] = '#';
$menu['1']['blank'] = 0;

$menu['1']['0']['label'] = 'Notes';
$menu['1']['0']['icon'] = '';
$menu['1']['0']['id'] = '';
$menu['1']['0']['class'] = '';
$menu['1']['0']['url'] = '/notes.php';
$menu['1']['0']['blank'] = 0;

$menu['1']['1']['label'] = 'Testing';
$menu['1']['1']['icon'] = '';
$menu['1']['1']['id'] = '';
$menu['1']['1']['class'] = '';
$menu['1']['1']['url'] = '/testing.php';
$menu['1']['1']['blank'] = 0;

$menu['2']['label'] = 'Resources';
$menu['2']['icon'] = 'fa-thumb-tack';
$menu['2']['id'] = '';
$menu['2']['class'] = '';
$menu['2']['url'] = '#';
$menu['2']['blank'] = 0;

Where $menu['0'], $menu['1'], etc. are all shown on the main nav menu. Any array underneath them, such as $menu['1']['0'] are all submenus under their parent.

I am trying to check each main element on the array to see if there is a sub-array (if there are any submenus to create).

foreach ($menu as $item) {

  if (is_array($item)) {

    foreach ($item as $subitem) {

      print_r($subitem); // See notes below

    }

  }

}

What I am trying to do with print_r($subitem) is come up with an array like:

$subitem['label'] = 'Notes';
$subitem['icon'] = '';
$subitem['id'] = '';
$subitem['class'] = '';
$subitem['url'] = '/notes.php';
$subitem['blank'] = 0;

Ideas?

  • 写回答

4条回答 默认 最新

  • doujia6433 2015-04-28 22:18
    关注

    You should reorganize your array:

    $menu[0] = ['items' => [], 'submenus' => []];
    

    So all menus have an items key and a submenus key. In the submenus array, you should put an array that looks identical (that is, has and 'items' key and 'submenus' key).

    That way you can just count() the submenus key and know if there are any submenus to create. It also will let you nest them as far as you want if you write a recursive function for the menu.

    For example:

    <?php
    // feed this an initialized menu, and an array of items
    function addMenuItem($menu, $options) {
        $newItem = makeMenu();
        $newItem['items'] = $options;
        $menu[] = $newItem;
        return $menu;
    }
    
    // the parent menu is first, submenu second
    function addSubMenu($menu, $pos, $subMenu) {
        $menu[$pos]['submenus'][] = $subMenu;
        return $menu;
    }
    
    // create a 'valid' but empty menu array
    function makeMenu() {
        return array('items' => array(), 'submenus' => array());
    }
    
    ?>
    

    As a side note, this is something that would work really well in Classes instead of functions and arrays.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿