Causes and How To Fix: “Parse error: syntax error, unexpected ‘foreach’ (T_FOREACH)…”

You cannot code a foreach inside an array declaration. Instead, populate an array with the structure you expect and then assign it (as you would a simple variable) to your array declaration

Do Not Do This!


//This is WRONG. Do not use this code
        'to' => array(

            foreach ( $deliver_to_arr as $single_email_prop) {
                 print_r( $single_email_prop );
            }
          
          ),

Do This Instead

//This is CORRECT. Use this code
       'to' => $deliver_to_arr,

References: