API how to combine email addresses from list field and checked checkboxes to send email?

hi there, I have been battling this one for a while and keep thinking Im on the verge of solving it but continually let down by my mediocre cut and paste PHP “skills”…
in summary I have a form which has a group of checkboxes showing email addresses, and a list field below which allows the user to add up to 10 more email addresses. I have a custom PHP function running which currently takes the addresses from the list field and inserts them into the BCC field of an email which successfully is sent out.
what I am now trying to do is also add the checked email addresses from the checkbox group to the BCC field, so the email is sent out to the list of recipients made from the combined output of the checkbox and list field.

currently the code is as follows:

$emails = $entry[1]; //field 1 is the list field
$emailRecipients = unserialize($emails);
$notification['bcc'] = implode(",", $emailRecipients);

I have tried a few different methods of extracting checked values from the checkbox field (field #4) and combining it into an array with field #1 but I think the issue is arising in not having the data formatted correctly. eg one attempt had

$emails1 = $entry[1]; //list field
$emails2 = $entry[4]; //checkbox field
$emailRecipients = merge_array($emails1,$emails2);

then I discovered that the list field output is a different array structure than the checkbox array so I attempted a few methods of serializing and unserializing but still cant combine them. I feel like it should be simple so can anyone set me straight? Much appreciated!

Gravity Forms has 2 awesome array functions: rgar and rgars.

Since both are flat arrays (if I’m not mistaken) you should be able to use rgar for this:

$list = rgar( $entry, '1' );
$checkbox = rgar( $entry, '4' );
$emailRecipents = merge_array( $list, $checkbox );
2 Likes

Thanks Hiranthi! and sorry for the delay in responding.
Im working on this and realised I made an error in my original post, its array_merge, not merge_array - but even so, this suggestion hasn’t worked for me - Im getting this error (similar to my previous attempts):

PHP Warning: array_merge(): Argument #1 is not an array

Going to keep tinkering but if you have any thoughts Im grateful
cheers,
Joe

Oh dear, I completely missed that! :grimacing:

What I usually do when I’m not sure what the outcome of a specific thing is (like the fields in this case), is print it out:

wp_die( print_r( rgar( $entry, '1' ), true ) ); exit; # list
wp_die( print_r( rgar( $entry, '4' ), true ) ); exit; # checkboxes

Depending on whether I may need it again, I copy/paste (and comment it out) it into the file I’m working with so I have the output-contents near when needed :slight_smile: