Modify Array () form HTML result

2

I have the following Array () return:

[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 feedback 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
            )               
    )

Follow 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?

    
asked by anonymous 01.12.2016 / 18:00

1 answer

5

Change your HTML structure 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 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; ?>">
    
01.12.2016 / 18:03