Format Array differently

2

I have the following array() :

Array
(
    [valor] => Array
        (
            [0] => 433.36
            [1] => 433.36
            [2] => 433.36
            [3] => 433.36
            [4] => 433.36
            [5] => 433.36
            [6] => 433.36
            [7] => 433.36
            [8] => 433.36
            [9] => 433.36
            [10] => 433.36
            [11] => 433.36
        )

    [data] => Array
        (
            [0] => 2015-10-21
            [1] => 2015-10-21
            [2] => 2015-10-21
            [3] => 2015-10-21
            [4] => 2015-10-21
            [5] => 2015-10-21
            [6] => 2015-10-21
            [7] => 2015-10-21
            [8] => 2015-10-21
            [9] => 2015-10-21
            [10] => 2015-10-21
            [11] => 2015-10-21
        )

    [detalhes] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
        )

)

But I need the return of this array, come in the following format:

Array
(
    [0] => Array
        (
            [campo1] => campo1
            [campo2] => campo2
            [campo3] => campo3
        )

    [1] => Array
        (
            [campo1] => campo1
            [campo2] => campo2
            [campo3] => campo3
        )
)

Form:

<form class="form-inline" method="post" action="<?php echo current_url(); ?>/adicionar">
    <fieldset>

        <div class="span12" style="height:35px;">
            <div class="span3" style="text-align:right">
                <strong>Registro</strong>
            </div>

            <div class="span9">
                 <? echo $lista_funcionario[0]->razaosocial; ?>
            </div>
        </div>

        <div class="span12" style="height:35px;">
            <div class="span3" style="text-align:right">
                <strong>Base de Cáculo</strong>
            </div>

            <div class="span9">
                 <? if(empty($this->input->post('base_calculo'))) echo "MANUAL"; else echo $this->input->post('base_calculo');  ?>
            </div>
        </div>


        <?
            foreach($lista_funcionario as $valor){

                /* Verificando se o tipo de parametro é vale */
                if($this->uri->segment(3)=="840"){
                    $base_calculo = str_replace("%", "", $this->input->post('base_calculo'));
                    $valor_adiantamento = $valor->salario * $base_calculo / 100;    
                }


        ?>
        <div class="span12" style="height:35px;">
            <div class="span3" style="text-align:right">
                <strong><? echo $valor->nome; ?></strong>
            </div>

            <div class="span1">
                <input id="campo_valor[]" class="span12" type="text" name="campo_valor[]" value="<? if(!empty($this->input->post('valor')) and $this->input->post('valor')!=$valor_adiantamento){ echo $this->input->post('valor'); } elseif(!empty($valor_adiantamento)){ echo $valor_adiantamento; } ?>" placeholder="0.00"  />
            </div>
            <div class="span2">
                <input id="campo_data[]" class="span12" type="date" name="campo_data[]"  value="<? if(!empty($this->input->post('data'))){ echo $this->input->post('data'); } ?>" />
            </div>
            <div class="span6">
                <input id="campo_detalhes[]" class="span10" type="text" name="campo_detalhes[]" placeholder="Detalhes do Provento"  />
            </div>
        </div>
        <? } ?>


        <div class="line" style="padding: 1%; margin-left: 0">
            <div class="span6 offset3" style="text-align: center">
                    <button class="btn btn-success" id="btnContinuar"><i class="icon-plus icon-white"></i> Adicionar</button>
                <a href="<?php echo base_url() ?>parametro" class="btn"><i class="icon-arrow-left"></i> Voltar</a>
            </div>
        </div>

    </fieldset>
</form>

Update: Following the idea of the response posted, it looks like this:

Array
(
    [0] => Array
        (
            [campo] => Array
                (
                    [0] => Array
                        (
                            [valor] => 433.36
                            [data] => 2015-10-21
                            [detalhes] => 
                        )

I would like to know how do I then access this data so that I can insert them into the database in this way?

    
asked by anonymous 23.09.2015 / 22:10

1 answer

2

In your foreach add the key $key and change the field names

<?php
foreach($lista_funcionario as $key => $valor){

    /* Verificando se o tipo de parametro é vale */
    if($this->uri->segment(3)=="840"){
        $base_calculo = str_replace("%", "", $this->input->post('base_calculo'));
        $valor_adiantamento = $valor->salario * $base_calculo / 100;    
    }
    ?>
    <div class="span12" style="height:35px;">
        <div class="span3" style="text-align:right">
            <strong><? echo $valor->nome; ?></strong>
        </div>

        <div class="span1">
            <input id="campo_valor[]" class="span12" type="text" name="campo[<?php echo $key;?>][valor]" value="<? if(!empty($this->input->post('valor')) and $this->input->post('valor')!=$valor_adiantamento){ echo $this->input->post('valor'); } elseif(!empty($valor_adiantamento)){ echo $valor_adiantamento; } ?>" placeholder="0.00"  />
        </div>
        <div class="span2">
            <input id="campo_data[]" class="span12" type="date" name="campo[<?php echo $key;?>][data]"  value="<? if(!empty($this->input->post('data'))){ echo $this->input->post('data'); } ?>" />
        </div>
        <div class="span6">
            <input id="campo_detalhes[]" class="span10" type="text" name="campo[<?php echo $key;?>][detalhes]" placeholder="Detalhes do Provento"  />
        </div>
    </div>
<?php
} 
?>

When the form submits, something like this will appear:

array(
    0=>array(
        'valor'=>'texto_do_campo_valor',
        'data'=>'texto_do_campo_data',
        'detalhes'=>'texto_do_campo_detalhes'
    ),
    1=>array(
        'valor'=>'texto_do_campo_valor',
        'data'=>'texto_do_campo_data',
        'detalhes'=>'texto_do_campo_detalhes'
    )
)

Doubt Update:

You can browse the post in this way

foreach($_POST[0]['campo'] as $VALUE){
    echo '<pre>';
    print_r($VALUE);
    echo '</pre>';
}
    
23.09.2015 / 22:20