How do I get all the menu items below a certain parent? I am currently working on a module that finds out the parent menu item by path, then displays the current tree structure of that related menu by finding the top parent and most important only displaying that menu item and submenus from within that menu.

path = "/system/menu/submenu";
parent = 'system';
output = parent + parent submenus. (other menu items excluded)


All menu items in "Custom menu":

- System
   - Menu wrapper
     - SubMenu 1A
     - SubMenu 2A
     - SubMenu 3A    
- Main
  - SubMenu 1B
  - SubMenu 2B
  - SubChildMenu 3B


PHP code should return this:

- System
   - Menu wrapper
     - SubMenu 1A
     - SubMenu 2A
     - SubMenu 3A    


My code (currently not working) is the following.

 $menu = 'custom_menu';
 $title = 'System';
 $tree = menu_tree_all_data($menu);
    foreach ($tree as $branch){
      if ($branch['link']['title'] == $title){
        $childtree = $branch['below'];
        break;
      }
    }
 return $childtree;


Notice: This needs to be PHP code only; all other modules don't support this only through manual selection.

People have asked this before but i've tried their solutions and no success.

Your help is very much appreciated.

https://stackoverflow.com/questions/5218861/restrict-menu-tree-to-first-level

https://stackoverflow.com/questions/2716787/how-to-get-all-the-menu-items-below-a-certain-parent-in-drupal

How do I display submenus separately from their menu tree?
I am using this function in my code for D7 to do this exact thing. Your code looks like it should work, but it is slightly different than what I have. Honestly I haven't visited this part of my code in a while. Hope this helps.

/**
* Returns an array of all menu items under a parent.
* @param string $title Menu item title.
* @param string $menu Menu name.
* @return array
*/
function MODULE_submenu_tree_all_data($title, $menu = 'main-menu') {
  $tree = menu_tree_all_data($menu);
  foreach ($tree as $branch) {
    if ($branch['link']['title'] == $title) {
      return $branch['below'];
    }
  }
  return array();
}