忽略 array_merge 中的空字符串
Ignore empty strings in array_merge
我有 3 个 if 语句(见代码)。每个用于创建电子邮件地址数组。如何将这 3 个字符串合并为 $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);
希望这对您有所帮助:
- 合并数组
- 遍历合并后的数组
- 检查当前元素是""还是null
- 如果是这样,用 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 function returns 如果变量已经定义则为真否则为假,所以用它来检查数组是否存在。要检查数组是否具有值,请使用比较运算符,如:sth equals another。删除关联数组或任何变量,如上文所述取消设置。
我知道我忘记了什么。我必须将数组声明为空,以防 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
我有 3 个 if 语句(见代码)。每个用于创建电子邮件地址数组。如何将这 3 个字符串合并为 $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);
希望这对您有所帮助:
- 合并数组
- 遍历合并后的数组
- 检查当前元素是""还是null
- 如果是这样,用 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 function returns 如果变量已经定义则为真否则为假,所以用它来检查数组是否存在。要检查数组是否具有值,请使用比较运算符,如:sth equals another。删除关联数组或任何变量,如上文所述取消设置。
我知道我忘记了什么。我必须将数组声明为空,以防 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