5

I'm having problems to iterate twice on the same array:

<? $indice=0 ?>
<?php foreach ($comisiones as $comision1):?>  
  <tr>  
    <td><?php echo ++$indice ?></td>  
    <td><?php echo tag('select',array('name'=>'comision_'.$indice),true)?>  
          <?php foreach ($comisiones as $comision2):?>  
            <option value="<?php echo $comision2->getId()?>">
               <?php echo $comision2->getNombre()." - ".$comision2->getDescripcion()?> 
            </option>
          <?php endforeach?> 
        </select>
    </td>
  </tr>
<?php endforeach?>  

The above code prints:

code result

And I'm expecting to see something like this (labels of the combos in the images are not the same, but I think the idea is clear):

expected results

Thanks in advance

4 Answers 4

9

My first instict is don't use foreach loops. I believe that PHP is using some internal pointers so the two foreach loops affect each other's position. Instead use a normal for loop.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Richard, I replaced the first foreach with a normal for and it worked. I can't believe that PHP has such a HUGE BUG in such a simple structure...
It's not a bug, it's documented as found on php.net/foreach: Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
whether it is documented, it is a BUG. That behavior is not intuitive, no one would expect that result from that piece of code. PHP should use different pointers for each foreach structure.
Sorry Neuquino, I have to agree with Pim Jager, as the foreach construct sits on top of the Iterator Pattern. You'd encounter the same behavior in any language which uses foreach - it's just become a little obscure as its been a while since any of us has had to manually write a foreach the old fashioned way... and even if it was a bug, we're talking about PHP here; I could easily believe that PHP would have such a huge bug :P
2

Based on your code it seems like you don't actually want a foreach loop in the outher loop. Just do a regular for loop from 0 to the size of the array. Something like this:

for ($i = 0; $i < count($comisiones); ++$i) {
    // Do what you want
}

1 Comment

yes, that would be one solution for my code. But I wanted to know I it was not working...
0

I belive thet the second loop should looks like or its related to

<?php foreach ($comision1 as $comision2): ?>

not

<?php foreach ($comisiones as $comision2): ?>  

otherwise you are not using $commision1 from first loop anyware

<?php foreach ($comisiones as $comision1): ?>  

1 Comment

I'm using it to iterate... it would be the same as a for loop from 1 to count($comisiones)...
0

Use normal for loops with two indexes, like this:

$len = count($comisiones);
for($i = 0; $i < $len; ++$i)
   for($j = 0; $j < $len; ++$j)

As stated clearly on PHP website:

"Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array." [source: http://www.php.net/manual/en/control-structures.foreach.php ]

Therefor your inner foreach loop resets every time the array pointer, that's why you are getting out only a terrible mess. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.