Fill Array with variables received via POST

0

I need to mount an array as 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 in this way:

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'];

Sent by this HTML that looks like this:

<input name='nDup[]' type='text'>
<input name='dVenc[]' type='text'>
<input name='vDup[]' type='text'>
    
asked by anonymous 26.10.2017 / 22:33

1 answer

0

Assuming the inputs are repeated the same number of times you can do this: 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 output:

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)
    
27.10.2017 / 01:58