Jess McKenzie
Jess McKenzie

Reputation: 8385

Foreach loop stripping all data from result

In the code below I have created a blacklist of e-mails that I would like to remove however I am getting an empty array when I run with if(stripos($row->guestEmail, $b))

If I remove the stripos and run with the basic if statement if($row->guestEmail) it shows all of the data including e-mail address's that are not in the $blacklist.

Why is the blacklist foreach stripping all of the data?

$guests = [];
$emails = [];
$blacklist = ['@booking.com', 'N/A', '[email protected]', '[email protected]'];
$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,
            );
        }
    }
}

Upvotes: 1

Views: 61

Answers (2)

Weltschmerz
Weltschmerz

Reputation: 2186

$guests = [];
$emails = [];
$blacklist = ['@booking.com', 'N/A', '[email protected]', '[email protected]'];
$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,
        );
    }
}

Upvotes: 1

Marat Badykov
Marat Badykov

Reputation: 844

Try this instead:

$guests = [];
$emails = [];
$blacklist = ['@booking.com', 'N/A', '[email protected]', '[email protected]'];
$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,
            );
        }
    }
}

Problem was in

stripos($row->guestEmail, $b) !== false 

This condition is the opposite of what you need.

Upvotes: 0

Related Questions