Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It does not work the way you outline with your pseudo code. However, the SPL offers a way to iterate multiple iterators at once. It's called <a href="http://php.net/MultipleIterator" rel="nofollow noreferrer"><code>MultipleIterator</code></a> and you can attach as many iterators as you like:</p> <pre><code>$multi = new MultipleIterator(); $multi-&gt;attachIterator(new ArrayIterator($array1)); $multi-&gt;attachIterator(new ArrayIterator($array2)); foreach($multi as $value) { list($key1, $key2) = $multi-&gt;key(); list($value1, $value2) = $value; } </code></pre> <p>See it in action: <a href="http://codepad.viper-7.com/bcOIJ7" rel="nofollow noreferrer">Demo</a></p> <p><strong>Edit:</strong> The first example shows a suggestion from the SPL. It has the benefit that it can deal with any kind of iterators, not only arrays. If you want to express something similar with arrays, you can achieve something similar with the classic <code>while(list()=each())</code> loop, which allows more expressions than <code>foreach</code>.</p> <pre><code>while ( (list($key1, $value1) = each($array1)) &amp;&amp; (list($key2, $value2) = each($array2)) ) { printf("%s =&gt; %s, %s =&gt; %s \n", $key1, $value1, $key2, $value2); } </code></pre> <p><a href="http://codepad.viper-7.com/ouFCZE" rel="nofollow noreferrer">Demo (the minimum number of elements are iterated)</a></p> <hr> <p>See as well a related question: <a href="https://stackoverflow.com/q/4157810/367456">Multiple index variables in PHP foreach loop</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload