How to set menu Display location

You can retrieve nav menu locations with get_nav_menu_locations

int[] Associative array of registered navigation menu IDs keyed by their location name. If none are registered, an empty array.

https://developer.wordpress.org/reference/functions/get_nav_menu_locations/

This refers to a theme mod internally named nav_menu_locations which can be used to update the locations.

You can modify that returned value and then save the new array like this:

// Update theme mod
set_theme_mod('nav_menu_locations', $nav_menu_locations);

Note that this won’t run filters and actions, caches may not be updated as a result, etc.

I’d also note that if you’re trying to create side menus for dynamically generated content that this does not scale, and you will run into issues. There are better ways to show content specific menus and sidebars that do not involve the creation of nav menus.