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条)

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面