Fill Array with variables received via POST

Asked

Viewed 919 times

0

I need to mount an array like the one below:

$aDup = array(
    array('carlos','2016-06-20','300.00'),
    array('mario','2016-07-20','300.00'),
    array('joao','2016-08-20','300.00'),
    array('silvio','2016-09-20','300.00')
);

To fill out below so:

foreach ($aDup as $dup) {
    $nDup = $dup[0];
    $dVenc = $dup[1];
    $vDup = $dup[2];
    $resp = $nfe->tagdup($nDup, $dVenc, $vDup);
}

Receiving this data via POST

$nDup = $_POST['nDup'];
$dVenc = $_POST['nDup'];
$vDup = $_POST['nDup'];

Uploaded by this html this so:

<input name='nDup[]' type='text'>
<input name='dVenc[]' type='text'>
<input name='vDup[]' type='text'>
  • Why did you put [ ] in the input name attribute?

  • Because it is an array and can be repeated these INPUTS, for example, can have more than 1 or 2 inputs with name nDup, dVenc, vDup

  • What is the real question? What difficulty are you encountering ?

  • in case you create an array within another correct array ?

  • tried it like this => <input name="nDup[duplicata][ ]">?

  • Guys, if you can pay attention to the question, I’ve separated what I need to do, so I have inputs that can be repeated 1 or more times, depending on the number of duplicates, with name='nDup[]' dVenc[] and vDup[], I’m getting them via POST, and I need to create an array with these POSTS equal to this $aDup = array( array('mario','2016-07-20','300.00'), array('Joao','2016-08-20','300.00') ); Since mario is nDup, the date on the side is dVenc and the value is vDup

Show 1 more comment

1 answer

0

Assuming the inputs are repeated the same number of times you can do so: html:

<form method="POST">
<div>
<input name='nDup[]' type='text'>
<input name='nDup[]' type='text'>
<input name='nDup[]' type='text'>
</div>
<div>
<input name='dVenc[]' type='text'>
<input name='dVenc[]' type='text'>
<input name='dVenc[]' type='text'>
</div>
<div>
<input name='vDup[]' type='text'>
<input name='vDup[]' type='text'>
<input name='vDup[]' type='text'>
</div>
<input type="submit">
</form>

php:

    <?php
$dados = [];

//supondo que os arrays $_POST nDup, dVenc e vDup tem tamanhos iguais
for($i=0;$i < count($_POST['nDup']);$i++){
    $dados[] = [
    isset($_POST['nDup'][$i]) ? $_POST['nDup'][$i] : null,
    isset($_POST['dVenc'][$i]) ? $_POST['dVenc'][$i] : null, 
    isset($_POST['vDup'][$i]) ? $_POST['vDup'][$i] : null];
}
var_dump($dados);

Producing the following result:

array (size=3)
  0 => 
    array (size=3)
      0 => string 'maria' (length=5)
      1 => string '2016-06-20' (length=10)
      2 => string '1' (length=1)
  1 => 
    array (size=3)
      0 => string 'joão' (length=5)
      1 => string '2016-06-20' (length=10)
      2 => string '2' (length=1)
  2 => 
    array (size=3)
      0 => string 'ana' (length=3)
      1 => string '2016-06-25' (length=10)
      2 => string '3' (length=1)

Browser other questions tagged

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