I've looked around online for the answer to my problem, but I can't seem to articulate myself well enough.

I have foreach statement, which sometimes outputs a large number of items to parse. However, I only want to grab three at random and parse them. If there are less than three entries, I obviously want them all to show.

Do I use the increment function? Thanks for your help.

    Would something like this solve your problem?

    if (sizeof($firstArray) <= 3) {
       // parse all values of $firstArray using a foreach
    }
    else {
       // select three random values using array_rand() and parse those
    }
    

      I might do something like:

      shuffle($array);
      $counter = 0;
      foreach($array as $value)
      {
         // do something with $value
         if(++$counter >= 3)
         {
            break;
         }
      }
      
        NogDog wrote:

        I might do something like:

        shuffle($array);
        $counter = 0;
        foreach($array as $value)
        {
           // do something with $value
           if(++$counter >= 3)
           {
              break;
           }
        }
        

        Thanks, that was exactly what I needed. I've always had trouble with incrementing.

        G

          Don't forget to mark this resolved, please.

            Write a Reply...