Jason
Jason

Reputation: 821

Comparing dynamic arrays together with an if then else statement

In my code I want to replace the following:

if($file['domain']=="VALUE" || $file['domain']=="VALUE" || $file['domain']=="VALUE" || $file['domain']=="VALUE"){}
else{}

with something like this that can be changed in a more generic config file:

$domains_to_exclude="VALUE,VALUE,VALUE,VALUE";

The values of both arrays change and vary. What I want to do is if the $file['domain'] matches the value of domains_to_exclude is to skip over it.

I am going in the right direction by trying something like this. So far I've not had any success.

$myArray = explode(',', $domains_to_exclude);
$count = count($file);
for ($i=1; $i<$count; $i++)
{
  if ($myArray[$i] !== $file['domain'])
  {
    $domain=$file['domain'];
    $domainn = str_replace("", "", $domain);
    echo'<option value="'.$domain.'">'.$domainn.'</option>';
  }
  else {}
}

Upvotes: 0

Views: 52

Answers (2)

Torbj&#246;rn Stabo
Torbj&#246;rn Stabo

Reputation: 779

$myArray = explode(',', $domains_to_exclude);
if (!in_array($file['domain'], $myArray)) {
    // Domain is ok, process file
}

in_array($str, $arr) checks if any of the values in $arr equals $str.

And also, you don't have to have that else block there if it is empty. But it won't affect your code negatively either.

Upvotes: 1

Dray
Dray

Reputation: 887

you can do something like this:

$domains_to_exclude = array(...); //make an array of your "VALUES"

$file = array('foo', 'bar'); // your $file array

if(count(array_intersect($domains_to_exclude, $file)) > 0){
    // at least a match was found
 }

Upvotes: 1

Related Questions