Treat a POST array from a table

Asked

Viewed 184 times

0

Good night, my friends.

I’m having a hard time processing some data. I created a table where <td> are dynamically powered when it passes through a foreach like this:

<table class="table table-striped table-bordered" id="tb-despesas">
    <thead>
        <tr>
           <th>Conta(s)</th>
           <th>Valor Cobrar</th>
           <th>Valor Real</th>
           <th>Referente</th>
           <th>Consumo</th>
           <th>Medidas</th>
           <th>Obs.</th>
        </tr>
    </thead>
    <tbody>
       <?php
            foreach($Despesas->getResult() as $Despesa):
            extract($Despesa);
       ?>
       <tr>
           <td name="nm_nome"><?=$nm_nome?></td>
           <td><input name="vl_valor[]" class="form-control" type="number"></td>
           <td><input name="vl_valorreal[]" class="form-control" type="number"></td>
           <td><input name="referente[]" id="dt" class="form-control" type="month"></td>
           <td><input name="consumo[]" class="form-control" type="text"></td>
           <td><input name="medidas[]" class="form-control" type="text"></td>
           <td><input name="obs[]" class="form-control" type="text"></td>
       </tr>
       <?php
          endforeach;
       ?>
    </tbody>
</table>

At this stage, due to a class I have that does the insertion into the database by array, I needed to receive this as:

array
   0 =>
     'vl_valor' => '123',
     'vl_valorreal' => '123',
     'referente' => '2018-11'
   1 =>
     'vl_valor' => '456',
     'vl_valorreal' => '456',
     'referente' => '2018-11'

But what I end up getting in my $_POST is something different because it brings everything together. Ends up like this:

array
  'vl_valor' => 
    array (size=74)
      0 => string '1234' (length=4)
      1 => string '2345' (length=4)
  'vl_valorreal' => 
    array (size=74)
      0 => string '1234' (length=4)
      1 => string '2345' (length=4)

Can anyone shed any light on how to treat it? I wanted to do some foreach for every line I found

1 answer

1


One solution to this is to rearrange the array to the way you’re already waiting, see how it would look:

function reorganizaArray($arr) {
    $arrOrganizado = [];
    for ($i = 0; $i < count($arr['vl_valor']); $i++) {
        $temp = [];
        foreach ($arr as $key => $value) {
            $temp[$key] = $value[$i];
        }
        $arrOrganizado[] = $temp;
    }
    return $arrOrganizado;
}

See working on Ideone.

  • I figured I’d have to do something like that, the problem is it’s not getting into my head what the right logic is. With the example you gave me, it would be with the array already in a model that I could use. But my current var_dump looks like this: https://ideone.com/82BhZQ

  • @Rafael, I used as an example the exact same array you put in the question, try to use this function there.

  • Yes, I tried, but the array I receive is different from the one used in the example. In your example the array is in the format of the first result I showed in the question, what I get is the second array of my question. This is the difficulty.

  • If you are sure that the values will always come in this format, you can use this solution: https://ideone.com/qijVay

  • Oh now yes. I even tried to count the results but I did not get it right. I will always receive the first parameter will always be filled. Thank you so much for your help.

Browser other questions tagged

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