Split an array returned by menu_tree_output I want to display an expanded menu and split it in two columns after a certain amount of items:


Right now I have the following in my hook_block_view:

$menuTree = menu_build_tree('my-menu');
$menuOutput = menu_tree_output($menuTree);
$block['content'] = array('#markup' =&gt; drupal_render($menuOutput));
return $block;


So I would have to put some extra logic between menu_tree_output and drupal_render splitting the array after 5 items and wrapping the result in something like &lt;div class="first_column"&gt;...

But dfb($menuOutput) gives me this huge array which I can't just array_chunk($menuOutput, 5).

Any idea how to handle this?
Just in case someone needs this: I found a solution altering menu_build_trees output:

$menuTree = menu_build_tree('my-menu');

// flatten out the hierarchic menu tree
foreach ($menuTree as $key=&gt;$value) {
    // if the item has children
    if (count($value['below']) &gt; 0 &amp;&amp; $value['link']['hidden'] != 1) {
        $flatMenu[$key]['link'] = $value['link'];
        // add a custom class to the first level items
        $flatMenu[$key]['link']['localized_options']['attributes']['class'] = array(
          'first-level-item',
        );
        // override nested menu items
        $flatMenu[$key]['below'] = array();
        // and add those to the new array
        $flatMenu = array_merge($flatMenu, $value['below']);
    // if the item has no children
    } elseif ($value['link']['hidden'] != 1) {
        $flatMenu[$key] = $value;
        $flatMenu[$key]['link']['localized_options']['attributes']['class'] = array(
          'first-level-item',
        );
    }
}

$markup = '';
$chunk = 5;
// split the new array in chunks and wrap them each with a numbered class
foreach (array_chunk($flatMenu, $chunk, true) as $key=&gt;$value) {
  dfb($value);
  $markup .= '&lt;div class="flatmenu-column-' . $key . '"&gt;';
  $markup .= drupal_render(menu_tree_output($value));
  $markup .= '&lt;/div&gt;'."\n";
}

$block['content'] = array('#markup' =&gt; $markup);
return $block;


It is a bit hacky but with some extra lines of CSS it works perfectly well!