在下面的代码中,我创建了一个电子邮件黑名单,我想删除这些电子邮件,但是当我使用if(stripos($row->guestEmail, $b))
运行时,我得到一个空数组
如果我删除stripos
并使用基本的 if 语句运行if($row->guestEmail)
它会显示所有数据,包括不在$blacklist
中的电子邮件地址。
为什么每个黑名单都会剥离所有数据?
$guests = [];
$emails = [];
$blacklist = ['@booking.com', 'N/A', 'n.c@nc.com', 'n.c@nc.com'];
$date = date('Y-m-d');
foreach ($results->data as $row) {
$emails[] = $row->guestEmail;
foreach ($blacklist as $b) {
if (stripos($row->guestEmail, $b) !== false && date('Y-m-d', strtotime($row->endDate)) == $date) {
$guests[] = array(
'FirstName' => $row->guestFirstName,
'LastName' => $row->guestLastName,
'email' => $row->guestEmail,
'country' => $row->guestCountry,
'check-in_date' => $row->startDate,
'check-out_date' => $row->endDate,
);
}
}
}
$guests = [];
$emails = [];
$blacklist = ['@booking.com', 'N/A', 'n.c@nc.com', 'n.c@nc.com'];
$date = date('Y-m-d');
foreach ($results->data as $row) {
$emails[] = $row->guestEmail;
//check for all blacklist flags
$blackListed = false;
foreach ($blacklist as $b) {
if (stripos($row->guestEmail, $b) !== false) {
$blackListed = true;
break;
}
}
//if all pass and date is good, we're good
if (!$blackListed && date('Y-m-d', strtotime($row->endDate)) == $date) {
$guests[] = array(
'FirstName' => $row->guestFirstName,
'LastName' => $row->guestLastName,
'email' => $row->guestEmail,
'country' => $row->guestCountry,
'check-in_date' => $row->startDate,
'check-out_date' => $row->endDate,
);
}
}
试试这个:
$guests = [];
$emails = [];
$blacklist = ['@booking.com', 'N/A', 'n.c@nc.com', 'n.c@nc.com'];
$date = date('Y-m-d');
foreach ($results->data as $row) {
$emails[] = $row->guestEmail;
foreach ($blacklist as $b) {
if (stripos($row->guestEmail, $b) === false && date('Y-m-d', strtotime($row->endDate)) == $date) {
$guests[] = array(
'FirstName' => $row->guestFirstName,
'LastName' => $row->guestLastName,
'email' => $row->guestEmail,
'country' => $row->guestCountry,
'check-in_date' => $row->startDate,
'check-out_date' => $row->endDate,
);
}
}
}
问题出在
stripos($row->guestEmail, $b) !== false
这种情况与您需要的相反。