Search code examples
phparrayscodeigniteractiverecordarray-column

Return a flat array of column values from a CodeIgniter active record query


im returning data back from my db using codeigniter's database class , is there anyway I can put it into the same array without having to do additional array_merge logic after the loop completes?

foreach ($saved_forms[0] as $key => $value) {
     $this->db->select('form_text');
     $this->db->from('form_labels');
     $this->db->where('form_label', $key);
     $query = $this->db->get();
        
     $form_names = $query->result_array();
     $form_titles[] = $form_names;
}

result array

[4] => Array
    (
        [0] => Array
            (
                [form_text] => Participant Name
            )    
    )    
[5] => Array
    (
        [0] => Array
            (
                [form_text] => Date of Birth
            )    
    )

What I want:

 [0] => Array
       (
        [form_text] => Participant Name
        [form_text] => Date of Birth
       )

Solution

  • Try this

    $query = $this->db->select('form_text')
             ->from('form_labels');
             ->where_in('form_label', array_keys($saved_forms[0]));
             ->get();
    
    foreach( $query->result as $label){
       $form_titles[] = $label->form_text;
    }
    

    It will produce a single array with the texts