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

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Neuquino
  • 11,580
  • 20
  • 62
  • 76

4 Answers4

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.

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
  • 3
    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... – Neuquino Mar 28 '10 at 14:43
  • 2
    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. – Pim Jager Mar 28 '10 at 14:48
  • 6
    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. – Neuquino Mar 28 '10 at 15:04
  • 1
    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 – Richard JP Le Guen Mar 28 '10 at 16:35
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
}
alexanderblom
  • 8,632
  • 6
  • 34
  • 40
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): ?>  
Marcin
  • 5,469
  • 15
  • 55
  • 69
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. :)

Marco Demaio
  • 33,578
  • 33
  • 128
  • 159