Modify Array() HTML form result

Asked

Viewed 56 times

2

I have the following return of Array():

[item] => Array
    (
        [data_vencimento] => Array
            (
                [0] => 2016-12-05
                [1] => 2016-12-07
                [2] => 2016-12-22
            )

        [itens] => Array
            (
                [0] => 150,00|1
                [1] => 114,00|2
                [2] => 85,00|3
            )

    )

But I need this comeback to go this way:

[item] => Array
    (
        [0] => Array
            (
                [data_vencimento] => 2016-12-05
                [itens] => 150,00|1
            )

        [1] => Array
            (
                [data_vencimento] => 2016-12-07
                [itens] => 114,00|2
            )

        [2] => Array
            (
                [data_vencimento] => 2016-12-22
                [itens] => 85,00|3
            )               
    )

Follows the structure of HTML

<table class="table table-bordered">
    <tr>
        <th width="50" class="text-center">#</th>
        <th>Item do Pacote</th>
        <th>Valor</th>
        <th>Vencimento</th>
        <th width="50" class="text-center"></th>
    </tr>
    <?php 
    if(count($pacotes_lista_itens)>0){
        foreach($pacotes_lista_itens as $valor){ ?>
        <tr>
            <td width="50" class="text-center"><?php echo $valor->id; ?></td>
            <td><?php echo $valor->nome; ?></td>
            <td>R$ <?php echo number_format($valor->valor_venda, "2", ",", "."); ?></td>
            <td width="200px">
            <input type="date" class="form-control" name="item[data_vencimento][]" id="item[data_vencimento][]" data-id="<?php echo $valor->id; ?>">            
            </td>
            <td width="50" class="text-center"><input type="checkbox" name="item[itens][]" id="item[itens][]" class="checados"value="<?php echo number_format($valor->valor_venda, "2", ",", "."); ?>|<?php echo $valor->id; ?>"></td>
        </tr>
    <?php 
        }
    } else {
    ?>
        <tr>
            <td colspan="5" class="text-center">Nenhum item foi adicionado a este pacote.</td>
        </tr>
    <?php } ?>
</table>

How do I get this result?

1 answer

5


Change the structure of your HTML to:

<table class="table table-bordered">
<tr>
    <th width="50" class="text-center">#</th>
    <th>Item do Pacote</th>
    <th>Valor</th>
    <th>Vencimento</th>
    <th width="50" class="text-center"></th>
</tr>
<?php 
if(count($pacotes_lista_itens)>0){
    foreach($pacotes_lista_itens as $i => $valor){ ?>
    <tr>
        <td width="50" class="text-center"><?php echo $valor->id; ?></td>
        <td><?php echo $valor->nome; ?></td>
        <td>R$ <?php echo number_format($valor->valor_venda, "2", ",", "."); ?></td>
        <td width="200px">
        <input type="date" class="form-control" name="item[<?php echo $i; ?>][data_vencimento]" id="item[data_vencimento][]" data-id="<?php echo $valor->id; ?>">            
        </td>
        <td width="50" class="text-center"><input type="checkbox" name="item[<?php echo $i; ?>][itens]" id="item[itens][]" class="checados"value="<?php echo number_format($valor->valor_venda, "2", ",", "."); ?>|<?php echo $valor->id; ?>"></td>
    </tr>
<?php 
    }
} else {
?>
    <tr>
        <td colspan="5" class="text-center">Nenhum item foi adicionado a este pacote.</td>
    </tr>
<?php } ?>

Basically you should add an index in the foreach:

foreach($pacotes_lista_itens as $i => $valor){

Then put this index to reference the items in the current loop:

<input type="date" class="form-control" name="item[<?php echo $i; ?>][data_vencimento]" id="item[data_vencimento][]" data-id="<?php echo $valor->id; ?>">


<input type="checkbox" name="item[<?php echo $i; ?>][itens]" id="item[itens][]" class="checados"value="<?php echo number_format($valor->valor_venda, "2", ",", "."); ?>|<?php echo $valor->id; ?>">
  • almost that... the only difference is that the items come within a new array, like this one: [0] => Array ( [due date] => 2016-12-06 [items] => Array ( [0] => 150,00|1 ) )

  • 1

    I believe that if I remove the brackets from the items, it should work... I’ll test :)

  • 1

    Yeah, sorry, I forgot to take the clasps, I’ve edited!

  • 1

    Perfect @juniorNunes, that’s right, thank you!

Browser other questions tagged

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