Return result_array codeigniter

Asked

Viewed 196 times

1

Having the following table:

tbl_event

`id` INT(11) NOT NULL AUTO_INCREMENT,
`descricao` VARCHAR(255) NULL DEFAULT NULL,
`evento_id` INT(11) NOT NULL,
`devedor_id` INT(11) NOT NULL,    
`negociacao_id` INT(11) NOT NULL,
`assessoria_id` INT(11) NOT NULL,
`complemento` VARCHAR(255) NULL DEFAULT NULL,

And I am generating the following array with the function below:

public function obter_devedor_evento($devedor_id, $id_negociacao_selecionada)
{    
        $this->db->from('tbl_atendimento_evento');
        $this->db->where('devedor_id',$devedor_id);
        $this->db->where('negociacao_id',$id_negociacao_selecionada);
        $query = $this->db->get();
        return $query->result_array();
}

Generated Array

Array
(
    [0] => Array
        (
            [id] => 3
            [descricao] => Envio de e-mail
            [devedor_id] => 1
            [evento_id] => 3
            [negociacao_id] => 3
            [assessoria_id] => 1
            [complemento] => Texto do complemento
        )

    [1] => Array
        (
            [id] => 4
            [descricao] => Recado
            [devedor_id] => 1
            [evento_id] => 4
            [negociacao_id] => 3
            [assessoria_id] => 1
            [complemento] => Texto do complemento
        )

)

I want to know how I can include the complement field after the view

Desired Array

Array
(
    [0] => Array
        (
            [id] => 3
            [descricao] => Envio de e-mail
            [devedor_id] => 1
            [evento_id] => 3
            [negociacao_id] => 3
            [assessoria_id] => 1
            [view][complemento] => Texto do complemento
        )

    [1] => Array
        (
            [id] => 4
            [descricao] => Recado
            [devedor_id] => 1
            [evento_id] => 4
            [negociacao_id] => 3
            [assessoria_id] => 1
            [view][complemento] => Texto do complemento
        )

)

1 answer

1


Use the function array_map and make the necessary adjustments, there is no move, each item must be created and removed when it is unnecessary, example:

public function obter_devedor_evento($devedor_id, $id_negociacao_selecionada)
{    
        $this->db->from('tbl_atendimento_evento');
        $this->db->where('devedor_id',$devedor_id);
        $this->db->where('negociacao_id',$id_negociacao_selecionada);
        $query = $this->db->get();
        return array_map(function($item){
            $item['view']['complemento'] = $item['complemento']; // adiciona nova chave
            unset($item['complemento']); // remove a chave que não precisa
            return $item;
        }, $query->result_array());
}
  • Um, perfect, it really worked. If you add other items to the view, how can it be done ?

  • I must repeat item and unset inside the array_map?

  • @Wagnerson the logic to add is $item['view']['nova _ chave'] = valor

  • @Wagnerfilho the logic to remove is unset($item['nome _ da _ chave']);

  • Okay, I tried and I got it. Thank you!!

  • Excuse the question, but it is possible to use unset in row_array ?

  • @Wagnerson in the result of it yes! is the same thing.

  • It’s because I’m getting this feedback: Cannot unset string offsets, doing as follows: return array_map(function($item){
 unset($item['dt_cadastro']);
 unset($item['dt_atualizacao']);
 unset($item['id']);
 return $item;
 },
 
 $query->row_array());

  • The $query->row_array() returns a simple array does not need to interact, because it is not a collection. place $item = $query->row_array(); then put unset($item['id']); that is, it does not need to array_map, maybe you don’t even have to do it already in SQL if you can remove what you don’t need...

  • Perfect, it worked. Thank you

Show 5 more comments

Browser other questions tagged

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