Increment a counter within a php foreach

Asked

Viewed 3,559 times

2

In a bow tie foreach 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 do 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++;
}
?>
  • I don’t know how I can increment a counter to set the tabindex

  • It seems to me a case of using a for, not foreach.

4 answers

4

According to the official PHP documentation, with each iteration, the value of the current element is assigned to $value and the array’s internal pointer moves forward one position (then, in the next iteration, if you are looking at the next element).

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

Edited

So, your piece of code 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++;

    }

  }

?>
  • In this format will return me only 0 E1 in both attributes tabindex of inputs, but as there are 2 inputs in the foreach loop need in a way that increments me and returns me in the attribute tabindex 1, 2, 3 and 4 as an example.

  • Have how, please, you post your array?

  • $addresses = array('cep' => '00000-00', 'street' => 'Street XXXX'), array('cep' => '11111-111', 'street' => 'Street YYYY'))

  • Above, I made the necessary changes to achieve the expected result.

0

To play what you imagine, just add one more increment to the 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++;
}
?>

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++;
}
?>
  • should be $c = 0; see ideone https://ideone.com/YaqVsD

  • Correct, corrected

0


Just work with one more variable to tabindex which increments 2 with each iteration of 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.