i have a foreach() function and i want to include an if() statement so that if one of the array items has a value = "x" then skip just that particular instance and continue to the next... anyone know how to break out of just the one instance and continue on? thanks!!!

    Use an if statement

    foreach($array as $key => $value) {
       if($value == "x") {
          // skip it
       }
    
    echo $key . ": " . $value . "<br>";
    
    }

    Cgraz

      hmm... does continue work in foreach()?

      If it does, then cgraz's example could be:

      foreach($array as $key => $value) {
      if ($value == "x") {
      	continue;
      }
      echo $key . ": " . $value . "<br>";
      }

        Wouldn't this work? Probably not lol.

        foreach($array as $key => $value)
        {
           if(!$value == "x")
           {
              echo $key . ": " . $value . "<br>";
           }
        }
        

        I'm no expert, but trying to learn... If this wouldn't work could somebody tell me why? 🙂

          probably would be clearer to write it as:

          foreach ($array as $key => $value)
          {
             if ($value !== "x")
             {
                echo $key . ": " . $value . "<br>";
             }
          }
            Write a Reply...