Re: Foreach question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Nov 15, 2007, at 7:50 PM, Juan Marcelo Rodríguez wrote:
2007/11/15, Philip Thompson <philthathril@xxxxxxxxx>:
On Nov 15, 2007 5:12 PM, Juan Marcelo Rodríguez <
juan@xxxxxxxxxxxxxxxxxxxxx>
wrote:
Yes, I made a mistake in the first sentence.

The code is :
[...]

foreach ($equipos as $key => $val){

echo "<tr><td>";
echo "1" . "</td><td>";  // I would like to add the counter here
reeplacing
"1"

echo "<tr><td>".($key+1)."</td><td>";

This is assuming $key starts at 0 and increments by 1 each iteration....
However, if you can't assume that, just have a separate counter:

<?php
$counter = 0;
foreach ($a as $b => $c) {
    echo "<tr><td>".(++$counter)."</td><td>";
...
}

very good idea.

It also assumes that the array in numerically indexed and corresponds to the value you expect. The counter is safer.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux