How to switch between two primary menus (programatically0

You can try this code in specific page template

function change_primary_menu( $menu ) {
        // you can check spesific menu id using this print and after that assign in,
        // and you can also check menu location using this print too
    echo '<pre>';
    print_r($menu) ;
        echo '</pre>';

        $user = wp_get_current_user();
        //in this example im check the user role is 'user'
        if ( in_array( 'user', (array) $user->roles ) ) {
            //if yes change to user menu (19 and 21 is just example menu id)
            $menu['primary'] = 19;
        }else{
            //if not change to another menu
            $menu['primary'] = 21;
        }
    return $menu;
};

add_filter( 'theme_mod_nav_menu_locations', 'change_primary_menu', 10, 2 );