Dynamically add sub menu items

I have found what I was looking for the function returns an id of the menu item needed.

    $my_menu_id = wp_update_nav_menu_item(4, 0, array('menu-item-title' => __($my_menu_title), 'menu-item-url' => home_url("https://wordpress.stackexchange.com/".$url."https://wordpress.stackexchange.com/"), 'menu-item-parent-id' => $my_menu_id, 'menu-item-status' => 'publish')); 

by knowing the menu id you can then create sub menus attached to the first menu item using the menu-item-parent-id