Create shortcodes within foreach loop (using array)

Solved issue of dynamic function name by using $shortcode var as function name

foreach ($shortcodes as $shortcode) {

  add_shortcode($shortcode, function ($atts) use ($shortcode) {

    $filepath = str_replace('_', '-', $shortcode);

    ob_start(); 
    require_once(get_stylesheet_directory() . '/template-parts/' . $filepath . '.php');
    return ob_get_clean();
  });
}