my highly nested foreach logic works upon first iteration only
<p>I have some nested foreach loops and I get funny and unexpected results. </p>

<p>On line 9 in my example code below, I am looping over "$servers as $server" and upon the first iteration of the main loop, it all works fine, I get the list of servers as expected, but for subsequent iterations, I don't. </p>

<p>The condition on line 7 however keeps returning true when it should "if ($synced['video_id'] == $v['id'])".</p>

<p>This leads me to ask - the array I call $servers - is it somehow used up after the first iteration, or do I need to reset it back to position 0 again for some reason?</p>

<pre><code>    //create array of servers synced with this video, also count them for later percentage readout.
    foreach ($videos as &amp;$v) {
        $syncArray = Array();
        $i = 0;
        foreach ($synced as $synced) {
            //find whether sync-entity pertains to this video
            if ($synced['video_id'] == $v['id']) {
                //now get name of synched server instead of id
                foreach($servers as $server) {
                    if ( $synced['server_id'] == $server['id'] ) {
                        $syncArray[$i] = $server['screenName'];
                        //if servers screen has a parent, put that in parenthesis for clarity
                        if ($server['screenParent']) {
                            $syncArray[$i] .= " (". $server['screenParent'] .")";
                        }                           
                    }
                }
                //increment for calculating percentage later
                $i++;
            }
        }
        //append the array of synced servers for this video to the videoarray
        $v['syncArray'] = $syncArray;
</code></pre>

<p>var_dump of $servers:</p>

<pre><code>array(3) {
[0]=&gt; array(4) { ["id"]=&gt; int(9) ["userName"]=&gt; string(20) "företag1/testskärm" ["screenName"]=&gt; string(9) "Gate C 22" ["screenParent"]=&gt; string(10) "Terminal 5" }
[1]=&gt; array(4) { ["id"]=&gt; int(15) ["userName"]=&gt; string(15) "företag1/entre" ["screenName"]=&gt; string(6) "Entré" ["screenParent"]=&gt; string(14) "Avgångshallen" }
[2]=&gt; array(4) { ["id"]=&gt; int(17) ["userName"]=&gt; string(14) "företag1/test" ["screenName"]=&gt; string(4) "test" ["screenParent"]=&gt; string(0) "" } }
</code></pre>
