How to turn a menu tree into an associative array? I've dig into Drupal menu core to find out a way to build an associative array from a menu tree(eg,the account menu) so that I can output the data as json format. But it seems not possible to do this with Drupal 8. The info I need from a menu tree include title, description, url, and I also need to know whether it has sub menu and if it's enabled. All this can be easily achieved using menu_build_tree() function in Drupal 7. Please understand I tried the so-called Drupal 8 equivalent of menu_build_tree() (\Drupal::menuTree();) but it just gives me large object using which I don't know how to extract the data I need.
You can load up the menu tree with MenuLinkTree::load, which will give you an array of menu links to work with. From there it's just a matter of looping through and getting what you need, e.g.

$tree = \Drupal::menuTree()-&gt;load('main', new \Drupal\Core\Menu\MenuTreeParameters());
foreach ($tree as $item) {
  $title = $item-&gt;link-&gt;getTitle();
  $url_obj = $item-&gt;link-&gt;getUrlObject();
  $has_children = $item-&gt;hasChildren;
  // etc...
}


The link is a MenuLinkInterface, each item itself is a MenuLinkTreeElement.

It would be preferable to inject the menu.link_tree service if possible, rather than getting it from \Drupal.