Help! We are trying to exclude elements in the first array from printing when we filter them against the second array. In the conditional statement, if you change the != to ==, it works the opposite way fine.

Cracked out in Columbus...

$bry = array("blue","red", "indigo");
$roygbiv = array("red","orange","yellow","green","blue","indigo","violet");
$bry_count = count($bry);
$roygbiv_count = count($roygbiv);
for ($x = 0; $x <= $bry_count; $x++) {
for ($y = 0; $y <= $roygbiv_count; $y++) {
if ($bry[$x] != $roygbiv[$y]) {
echo $roygbiv[$y];
}
}
}

    first of all, your second array contains all the elements that first array does, so this should print nothing, here's a corrected example. I removed indigo from the 2nd array.

    $bry = array("blue","red", "indigo");
    $roygbiv = array("red","orange","yellow","green","blue","violet");
    $bry_count = count($bry);
    $roygbiv_count = count($roygbiv);
    
    for ($x = 0; $x <= $bry_count; $x++) {
        for ($y = 0; $y <= $roygbiv_count; $y++) {
            if ($bry[$x] == $roygbiv[$y]) {
                continue(2);
            }
        }
        echo $bry[$x] ."\n";
    }

      Aha! Yes! Continue! Man, I think I need a new book. Both books we have around here don't include Continue. Thanks very much. Very useful!

      Marty

        You could also consider:

        $bry = array("blue","red", "indigo");
        $roygbiv = array("red","orange","yellow","green","blue","violet");
        $filtered_array = array_diff($bry, $roygbiv);
        
        foreach($filtered_array as $colour)
        { echo $colour."\n";
        }
        
          Write a Reply...