在PHP问题嵌套的foreach [英] nested foreach in PHP problem
问题描述
< ;? $ 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?>
上面的代码打印出来:
< a href =http://img440.imageshack.us/img440/7732/res.png>代码结果http://img440.imageshack.us/img440/7732/res.png
我期望看到类似这样的东西(图片中的组合标签不一样,但我认为这个想法很清楚):
预期成果http://img229.imageshack.us/img229/7969/esperado。 png
预先致谢 instict是不使用 foreach
循环。我相信PHP正在使用一些内部指针,因此两个 foreach
循环会影响彼此的位置。而是使用正常的循环。
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 http://img440.imageshack.us/img440/7732/res.png
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 http://img229.imageshack.us/img229/7969/esperado.png
Thanks in advance
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.
这篇关于在PHP问题嵌套的foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!