Frank Thornton Frank Thornton - 3 months ago 18
PHP Question

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 {}
}

Answer
$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.