如何在没有重新声明问题的情况下在foreach循环中使用函数或将其置于foreach循环之外?

I am building an Admin page for my themes. In the admin page, it will be possible for admins to go to the options page and select how many custom post types they want to open. I've already got the first part running (the page where they put in how many custom post types they want, and what the names of those custom post types should be). I am having trouble with the second part: using a foreach loop to get information that has been collected in the first step and putting it through a function to register that post type.

  • When using a regular function, it will get a fatal error about not being able to redeclare the same function.
  • When putting the function outside of the foreach-loop, I will not be able to get the information per loop-item.
  • Putting the function inside of a variable has me wondering how I can still use a functionname to have an add_action hook it into WordPress.
  • I cannot find a solution that works, or that at least I understand. I am about usually able to handle PHP quite alright, especially with regards to working in WordPress. And although the problem feels simple, the solution might be way over my head.

Start of the foreach-loop:

foreach($custom_post_type_select as $custom_post_type) {
    $custom_post_type_number = $custom_post_type_integer+1;

    $custom_post_type_name = "custom_post_type_name_$custom_post_type_number";
    $register_custom_post_type_name = $custom_post_type_selection[$custom_post_type_name];

    $custom_post_type_marker = "custom_post_type_activate_$custom_post_type_number";
    $register_custom_post_type_activate = $custom_post_type_selection[$custom_post_type_marker];

Registering the custom post type:

function register_custom_post_type() {
$args = array(
    'public' => true,
    'labels' => array(
        'name' => $register_custom_post_type_name,
        'singular_name' => $register_custom_post_type_name,
    'public' => true,
    'has_archive' => true,
    'supports' => array('title'),
    'show_ui' => true);
register_post_type($register_custom_post_type_name,$args);};

if(stripos($register_custom_post_type_activate,'yes') !== false) {
    add_action('init','register_custom_post_type');}

And the end of the foreach-loop:

if(++$custom_post_type_integer == $custom_post_type_variable)
    break;}}

How would it be possible to both use the foreach-loop to get the right information and a function that is being called by an add_action?