微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

忽略array_merge中的空字符串

如何解决忽略array_merge中的空字符串

我有3条if语句(请参阅代码)。每个用于创建电子邮件地址数组。如何将这三个字符串合并到$to中(知道它们可能为空或什至不存在)?

显然这不起作用...

    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);

解决方法

希望这对您有帮助:

  1. 合并数组
  2. 遍历合并数组
  3. 检查当前元素是否为“”或为空
  4. 如果这样,请使用array_splice()删除它
,

$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函数将返回true,否则返回false,因此请使用它来检查数组是否存在。要检查数组是否有值,请使用比较运算符,例如:sth等于另一个。删除关联数组或任何未设置如上所述的变量。

,

我知道我忘记了什么。如果没有触发 if语句,我必须将数组声明为空。检查下面的我的工作代码:

    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);
,

//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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。