2

Is there a simple way that I can create an array within my foreach to allow me to show how many items from the blacklist have been removed?

Result Expected:

guest.booking.com 5

N/A 10

etc

Code:

$blacklist = ['@guest.booking.com', '@booking.com', 'N/A', '[email protected]', '[email protected]'];
$blacklistedEmails = false;

    foreach ($blacklist as $b) {

        if (stripos($row->guestEmail, $b) !== false) {
            $blacklistedEmails = true;
            break;
        }
    }
2
  • do you want to count the occurrence of array_item Commented Oct 3, 2018 at 23:43
  • you could also $i = 0; outside the foreach and then $i++; inside the if. Commented Oct 3, 2018 at 23:59

4 Answers 4

1

Just collect the matches in an associative-keyed result array. You don't seem interested in the 0 count elements, so they are not generated in the output.

Checking isset() is important to avoid generating Notices on the first occurrence of a matched blacklist value.

Code: (Demo)

$rows = [
    (object)["guestEmail" => "[email protected]"],
    (object)["guestEmail" => "[email protected]"],
    (object)["guestEmail" => "[email protected]"],
    (object)["guestEmail" => "n/a"],
    (object)["guestEmail" => "[email protected]"],
    (object)["guestEmail" => "[email protected]"]
];
$blacklist = ['@guest.booking.com', '@booking.com', 'N/A', '[email protected]', '[email protected]'];

foreach ($rows as $row) {
    foreach ($blacklist as $b) {
        if (stripos($row->guestEmail, $b) !== false) {
            if (isset($blacklistedcounts[$b])) {  // this important to avoid Notices
                ++$blacklistedcounts[$b];  // increment after the element key exists
            } else {
                $blacklistedcounts[$b] = 1;  // set 1 on first occurrence
            }
            break;  // no need to check for other matches for this guestEmail
        }
    }
}
var_export($blacklistedcounts);

Output:

array (
  '@booking.com' => 4,
  'N/A' => 1,
)

If for some reason you wish to see the zero values too, you can use array_fill_keys(), avoid the isset() conditional, and just use ++ incrementation syntax.

$blacklistcounts = array_fill_keys($blacklist, 0);
... then inside the stripos() condtion ...
++$blacklistedcounts[$b];
Sign up to request clarification or add additional context in comments.

Comments

0

To get the count of the blacklisted emails, you'll need to remove the break inside your for loop, like this:

$blacklist = ['@guest.booking.com', '@booking.com', 'N/A', '[email protected]', '[email protected]'];
$blacklistedEmails = false;
$blacklistCount = 0;

    foreach ($blacklist as $b) {

        if (stripos($row->guestEmail, $b) !== false) {
            $blacklistedEmails = true;
            $blacklistCount++;
        }
    }

This will give you the count of the blacklisted emails in $blacklistCount. If you need an array of all the blacklisted items, you could do this:

$blacklist = ['@guest.booking.com', '@booking.com', 'N/A', '[email protected]', '[email protected]'];
$blacklistedEmails = false;
$blackEmails = [];

    foreach ($blacklist as $b) {

        if (stripos($row->guestEmail, $b) !== false) {
            $blacklistedEmails = true;
            $blackEmails[] = $row->guestEmail;
        }
    }

This would give you an array of blacklisted emails in $blackEmails. Then you could just use:

$blacklistCount = count($blackEmails);

That would give you the array and a count.

1 Comment

These snippets should not be used. The first will generate Notices and the second doesn't store the element from the blacklist (which is what the OP asked for). Furthmore, the break is an important component as a matter of best practices.
0

Not sure if this is what you mean, because its pretty basic but code below seems to do what you want.

<?php
$blacklist = ['@guest.booking.com', '@booking.com', 'N/A', '[email protected]', '[email protected]'];
$blacklistedEmails = false;

$blacklistCount = array();
foreach ($blacklist as $b){
    $blacklistCount[$b] = 0;;
}

foreach ($blacklist as $b) {
    if (stripos("[email protected]", $b) !== false) {
        $blacklistCount[$b] += 1;
        break;
    }
}
 print_r($blacklistCount);

?>

Produces this output..

Array
(
    [@guest.booking.com] => 0
    [@booking.com] => 1
    [N/A] => 0
    [[email protected]] => 0
    [[email protected]] => 0
)

Obviously you need to loop through your email list to count the hits on each blacklisted item. I have just shown one example.

Comments

0

I'm assuming you have a bunch of rows that you are running through this check (otherwise I don't see how you'd ever have a count > 1).

A simple way to get the count of the hits for each email is to create an associative array using the blacklisted email as the key and the count as the value. When you iterate through your loop, just increment the value of the key.

$blacklist = ['@guest.booking.com', '@booking.com', 'N/A', '[email protected]', '[email protected]'];
$blacklistedEmailCounts = [];


foreach ($rows as $row){
    foreach ($blacklist as $b) {

        if (stripos($row->guestEmail, $b) !== false) {
            $blacklistedEmailCounts[$b]++;
        }
    }
}

To output the values you could just do something like:

foreach($blacklistedEmailCounts as $email=>$count){
    echo "$email: $count" . '<br>';
}

2 Comments

This solution should not be used because it will generate Notices.
It's simple..! And could easily be changed to avoid notices.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.