$ key starting with 1

0

Good afternoon guys, how do I start a $ key from a foreach by 1?

foreach ($dados as $key => $reg) {
$table .= '<tr>
           <td width="3%">' . $key . '</td>
           <td width="18%">' . $reg['nome_clientet'] . '</td>
</tr>';

Starting with zero, I would like to start from '1';

    
asked by anonymous 10.06.2016 / 19:40

2 answers

1

It seems that 1 would be more in the view because it's putting in the table so using a +1 would solve the print.

foreach ($dados as $key => $reg) {
    $newKey = $key +1;
    $table .= '<tr>
           <td width="3%">' . $newKey . '</td>
           <td width="18%">' . $reg['nome_clientet'] . '</td>
</tr>';

So you would use $newKey on prints and $key on the backend.

Or for the array to actually start from scratch should work like this:

array_push($array[0], $array); #adiciona o primeiro elemento como ultimo (para nao perder dados)
unset($array[0]); #apaga primeiro elemento do array.
    
10.06.2016 / 22:17
0

You can trim the array first before entering the foreach.

$reg = array_slice($reg, 1);
foreach ($dados as $key => $reg) {
    $table .= '<tr>
           <td width="3%">' . $key . '</td>
           <td width="18%">' . $reg['nome_clientet'] . '</td>
    </tr>';
    
10.06.2016 / 19:47