Compare array fields codeigniter

1

Good evening!

I am doing export reports, in my form are sent via $ _POST the fields that the user wants to export, I get and assign to a variable all fields. I need to compare these fields with the fields coming from the database return.

Array coming from the form

    Array
(
    [0] => razaoSocial
    [1] => data
)

Array from the Database

Array
(
    [0] => Array
        (
            [chamado_codigo] => 1
            [data] => 2015-09-01
            [razaoSocial] => Cooperativa de Crédito
            [cliente_cidade] => Chapeco
            [tecnico_nome] => jacomasio
        )

    [1] => Array
        (
            [chamado_codigo] => 2
            [data] => 2015-09-02
            [razaoSocial] => Cooperativa de Crédito
            [cliente_cidade] => Chapeco
            [tecnico_nome] => Eduardo
        )

)

Controller

public function exportaRelatorio(){

        $this->load->model('relatorios_model');

        $filtro = $_POST['filtro'];
        $de = $_POST['de'];
        $ate = $_POST['ate'];
        $campos = $_POST['campos'];

        foreach ($campos as $campo => $valor) {
            $dados[$campo] = $valor;
        }

        $retorno = $this->relatorios_model->ChamadosCliente($filtro, 
            converteData($de), 
            converteData($ate));
}

Model

public function ChamadosCliente($filtro, $de, $ate){
        $this->db->select('chamados.id as chamado_codigo,
            chamados.data_abertura as data,
            clientes.razaoSocial,
            clientes.cidade as cliente_cidade,
            usuarios.nome as tecnico_nome');
        $this->db->from('chamados');
        $this->db->join('clientes', 'chamados.cliente_id = clientes.id');
        $this->db->join('usuarios', 'chamados.tecnico_id = usuarios.id');
        $this->db->where('clientes.id', $filtro);
        $this->db->where("chamados.data_abertura BETWEEN '$de' AND '$ate'");
        return $this->db->get()->result_array();
    }

In this case, for example, I wanted to export only the reasonsocial and date fields, which the user filtered.

How can I do this?

Thank you!

    
asked by anonymous 26.09.2015 / 01:48

1 answer

1

Take a look, see if this solves:

<?php
$arrFields =  Array(
    "razaoSocial",
    "data"
);

$arrData = Array(
    0 => Array (
        "chamado_codigo"    => 1,
        "data"              => '2015-09-01',
        "razaoSocial"       => 'Cooperativa de Crédito',
        "cliente_cidade"    => 'Chapeco',
        "tecnico_nome"      => 'jacomasio'
    ),
    1 => Array (
        "chamado_codigo"    => 2,
        "data"              => '2015-09-02',
        "razaoSocial"       => 'Cooperativa de Crédito',
        "cliente_cidade"    => 'Chapeco',
        "tecnico_nome"      => 'Eduardo'
    )
);

foreach ($arrData as $key => $val):
    print '<br />'. $key .' | '. $val;  
    foreach ($arrFields as $fld):
        print ' ---- '. $fld .' : '. $val[$fld];
    endforeach ;
endforeach;
?>
    
26.09.2015 / 19:32