0

I have 3 if statements (see code). Each for creating an array of email addresses. How can I merge those 3 strings into a $to (knowing that they could be empty or doesn't even exist)?

Apparently this doesn't work...

    if ( in_array('client', $measurement_mail_report_recipients) ) {

        $measurement_client_id = intval($_POST['acf']['field_5e147914518a6']);
        $list1 = array();

        if (have_rows('company_email_addresses', $measurement_client_id)) {
            while (have_rows('company_email_addresses', $measurement_client_id)) {
                the_row();
                $list1[] = get_sub_field('company_email_address');
            }
        }

    }

    if ( in_array('contact', $measurement_mail_report_recipients) ) {

        $measurement_contact_id = intval($_POST['acf']['field_5e149714d044e']);
        $list2 = array();

        if (have_rows('contact_email_addresses', $measurement_contact_id)) {
            while (have_rows('contact_email_addresses', $measurement_contact_id)) {
                the_row();
                $list2[] = get_sub_field('contact_email_address');
            }
        }

    }

    if ( in_array('extra', $measurement_mail_report_recipients) ) {

        $measurement_mail_extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];

        if ( $measurement_mail_extra_recipients ) {
            $list3 = array();
            foreach( $measurement_mail_extra_recipients as $measurement_mail_extra_recipient ) {
                $list3[] = $measurement_mail_extra_recipient['field_5f71d55aaf382'];
            }
        }

    }

    $to         = array_merge($list1, $list2, $list3);
Yorlinq
  • 131
  • 14
  • Filter them while adding with an empty check. You could also add and then filter but that's extra work. – nice_dev Oct 09 '20 at 16:09
  • Heard of [`array_filter`](https://www.php.net/manual/en/function.array-filter.php)? *" If no callback is supplied, all empty entries of array will be removed. See empty() for how PHP defines empty in this case."* ... Simply `$to = array_filter($to);` should do it. – Markus AO Oct 09 '20 at 16:24
  • Does this answer your question? [Remove empty array elements](https://stackoverflow.com/questions/3654295/remove-empty-array-elements) – Markus AO Oct 09 '20 at 16:25
  • let me try just that. I'll let you know. – Yorlinq Oct 10 '20 at 05:28
  • I've tried: $recipients = array_filter($list1, $list2, $list3); $to = array_merge($recipients); (and also the way around so 1st merge, 2nd filter) but no luck. – Yorlinq Oct 10 '20 at 05:41
  • I little rephrased the question: "knowing that they could be empty or doesn't even exist" Maybe this makes more sense? – Yorlinq Oct 10 '20 at 06:19
  • Declare the empty arrays outside of the conditions and everything will merge just fine. – mickmackusa Oct 10 '20 at 08:46

4 Answers4

0

Hopefully this helps you out:

  1. Merge the arrays
  2. Loop through the merged array
  3. Check if current element is "" or null
  4. If so delete it with array_splice()
AntonioSk
  • 528
  • 3
  • 20
0

$myArray = [];
$myArray2 = [];
// If undefined the value becomes false instead of error (always do this)
$myArray2["value1"] = $_POST["post-value"] ?? false;

// Always use isset to check if a variable is defined
if(isset($myArray["some-value"])){
    // do some work
}
// proceed to remove an associated array or any variable
unset($myArray2["value1"]);// You can also unset any variable

PHP isset function returns true if a variable has been defined otherwise false, so use it to check if the array exists. To check if the array has value, use comparison operator like: sth equals another. to delete an associative array or any variable unset it as described above.

Dharman
  • 30,962
  • 25
  • 85
  • 135
smacaz
  • 148
  • 8
0

I know what I forgot. I had to declare the array empty in case the if statement wasn't triggered. Check my working code below:

    if ( in_array('client', $measurement_mail_report_recipients) ) {

        $measurement_client_id = intval($_POST['acf']['field_5e147914518a6']);
        $list1 = array();

        if (have_rows('company_email_addresses', $measurement_client_id)) {
            while (have_rows('company_email_addresses', $measurement_client_id)) {
                the_row();
                $list1[] = get_sub_field('company_email_address');
            }
        }

    } else { $list1 = array(); }

    if ( in_array('contact', $measurement_mail_report_recipients) ) {

        $measurement_contact_id = intval($_POST['acf']['field_5e149714d044e']);
        $list2 = array();

        if (have_rows('contact_email_addresses', $measurement_contact_id)) {
            while (have_rows('contact_email_addresses', $measurement_contact_id)) {
                the_row();
                $list2[] = get_sub_field('contact_email_address');
            }
        }

    } else { $list2 = array(); }

    if ( in_array('extra', $measurement_mail_report_recipients) ) {

        $measurement_mail_extra_recipients = $_POST['acf']['field_5f71d4eaaf381'];

        if ( $measurement_mail_extra_recipients ) {
            $list3 = array();
            foreach( $measurement_mail_extra_recipients as $measurement_mail_extra_recipient ) {
                $list3[] = $measurement_mail_extra_recipient['field_5f71d55aaf382'];
            }
        }

    } else { $list3 = array(); }

    $recipients = array_merge($list1, $list2, $list3);
    $to         = array_filter($recipients);
Yorlinq
  • 131
  • 14
  • Setting the empty array twice, in all three cases is unnecessary. There will be nothing to mop up if the empty arrays are unconditionally declared. I wouldn't use any of these answers. – mickmackusa Oct 10 '20 at 08:46
0

//Use this method
$array1 = [0,1,3,6];
$array2 = [];
$array3 = ["a","b","c"];
/*
 Store all the collective array in one array 
 the array_1,2 style will help to resolve things inside for loop
*/
$biggerArray = ["array_1"=>$array1, "array_2"=>$array2, "array_3"=>$array3];
$bigger2 = [];
for($i = 0; $i < 3; $i++){
    foreach($biggerArray["$i"] as $k => $v){
        if(!$biggerArray["$i"][$k]){
            // the value is empty
        }else{
            // it's not empty
            if(!isset($bigger2["array_$i"]){
                $bigger2["array_$i"] = [];
            }else{
                array_push($bigger2["array_$i"],$biggerArray["$i"][$k]);
            }
        }
    }
}
// with function return $bigger2 array
smacaz
  • 148
  • 8