zuk1
zuk1

Reputation: 18369

Print all values from a flat array unless value contains a specific substring

I have an array which is a list of domains, I want to print all of the items in the array except the one which contains $x.

$x is variable so basically it never prints the array item when it contains $x.

Upvotes: 2

Views: 189

Answers (1)

Ian
Ian

Reputation: 4258

Darkey's answer is correct assuming you don't want to print array values that equal $x - but if you don't wish to print those that "contain" $x, try:

foreach ($array as $key => $value) {
    if (strpos($value, $x) === false) {
         print($value);
    }
}

If $x = stack, then entries such as "stackoverflow.com" and "getstacked.org" would be excluded

Upvotes: 7

Related Questions