title: PHP loop over every second element of array
body: <p>I have an array of data and in this array there is another array for products. And I want to loop over every item of the product array and execute a specific function (no problem with this), but for every second item I want to execute a different function.</p>
  <p>The Array looks like this:</p>
  <p><em>demoData.php</em></p>
  <pre><code>&lt;?php
  return array(
    'documentInfo' =&gt; array(
        &quot;demoSubject&quot;,
        &quot;demoTitel&quot;,
        &quot;demoCreator&quot;,
        &quot;demoAuthor&quot;,
        &quot;demoKeywords&quot;,
    ),
    'products' =&gt; array(
       &quot;product one&quot;,
       &quot;product two&quot;,
       &quot;product three&quot;,
       &quot;product four&quot;,
       &quot;product five&quot;,
    )
  );
  ?&gt;
  </code></pre>
  <p>The function I want to execute for every second array item is the following (PDFlib textflow function with bold text):</p>
  <pre><code>$tf = $p-&gt;add_textflow($tf, /** item of $products */ . &quot; &quot;, $optlistMedium);
  </code></pre>
  <p>And for the other items I want to execute the following function (it's the same function generally speaking, but with normal text):</p>
  <pre><code>$tf = $p-&gt;add_textflow($tf, /** item of $products */ . &quot; &quot;, $optlistNormal);
  </code></pre>
  <p>If I understand this correct I can do it with a foreach loop for products and then in this foreach loop I put an if statement, am I correct? Something like this:</p>
  <pre><code>foreach($array['products'] as $products) {
    if(/** Every second array item */) {
       /** Execute function for bold text */
    } else {
       /** Execute function for normal text */
    }
  }
  </code></pre>
  <p>Though I don't really understand how I can get it working, so that the if statement gets every second element of the array. Maybe someone can help me with this.</p>