Increment a counter inside a foreach in php

3

In a foreach loop, I need to increment a counter to set the tabindex of the form fields so that the result is:

<input type="text" name="endereco[0][cep]" value="00000-000" tabindex="1">
<input type="text" name="endereco[0][rua]" value="Rua XXXX" tabindex="2">
<input type="text" name="endereco[1][cep]" value="11111-111" tabindex="3">
<input type="text" name="endereco[1][rua]" value="Rua YYYY" tabindex="4">

What I have:

<?php
$i = 0;

foreach ($enderecos as $endereco) {
?>
    <input type="text" name="endereco[<?php echo $i?>][cep]" value="<?php echo $endereco['cep']; ?>" tabindex="">
    <input type="text" name="endereco[<?php echo $i?>][rua]" value="<?php echo $endereco['rua']; ?>" tabindex="">
<?php
    $i++;
}
?>
    
asked by anonymous 03.11.2017 / 20:50

4 answers

0

Just work with one more variable for tabindex which increments 2 every iteration of the foreach

See working on ideone

$i = 0;
$j = 0;
  $enderecos = array(array('cep' => '00000-00', 'rua' => 'Rua XXXX'), array('cep' => '11111-111', 'rua' => 'Rua YYYY'));

foreach ($enderecos as $endereco) {
?>
    <input type="text" name="endereco[<?php echo $i?>][cep]" value="<?php echo $endereco['cep']; ?>" tabindex="<?php echo ($j+1) ?>">
    <input type="text" name="endereco[<?php echo $i?>][rua]" value="<?php echo $endereco['rua']; ?>" tabindex="<?php echo ($j+2) ?>">
<?php
    $i++;
    $j+=2;
}

See another example in ideone

another example in ideone

    
04.11.2017 / 02:17
4

According to the official PHP documentation , at each iteration, the value of the current element is assigned to $ value and the internal pointer of the array advances one position (then, in the next iteration, it will be looking at the next element).

foreach (array_expression as $key => $value)
    statement

Edited

So your code snippet would look like this:

<?php

  $enderecos = array(array('cep' => '00000-00', 'rua' => 'Rua XXXX'), array('cep' => '11111-111', 'rua' => 'Rua YYYY'));

  $i = 1;

  foreach($enderecos as $indice=>$endereco) {

    foreach($endereco as $key=>$value) {
?>
      <input type="text" name="endereco[<?php echo $indice;?>][<?php echo $key?>]" value="<?php echo $value; ?>" tabindex="<?php echo $i;?>" />
<?php
      $i++;

    }

  }

?>
    
03.11.2017 / 21:15
0

To reproduce what you imagine, you simply add one more increment to foreach ().

<?php
$i = 0;
$j = 0;
$tab = 1;
foreach ($enderecos as $endereco) {
?>
    <input type="text" name="endereco[<?php echo $i?>][cep]" value="<?php echo $endereco['cep']; ?>" tabindex="<?php echo $tab; ?>">
    <input type="text" name="endereco[<?php echo $j?>][rua]" value="<?php echo $endereco['rua']; ?>" tabindex="<?php echo $tab; ?>">
<?php
    $i++;
    $j++;
    $tab++;
}
?>
    
03.11.2017 / 21:12
0
<?php
$i = 1;
$c = 0;
foreach ($enderecos as $endereco) {
    $j = $i + 1;
?>
<input type="text" name="endereco[<?php echo $c;?>][cep]" value="<?php echo $endereco['cep']; ?>" tabindex="<?php echo $i; ?>">
<input type="text" name="endereco[<?php echo $c;?>][rua]" value="<?php echo $endereco['rua']; ?>" tabindex="<?php echo $j; ?>">
<?php
    $i+=2; // incrementar 2
    $c++;
}
?>
    
03.11.2017 / 22:11