phparrayscodeignitermultidimensional-arrayresultset

How to get a column of values from a CodeIgniter query resultset


I'm using this query:

$lang is like this Array ( [0] => Afrikaans [1] => English )

$this->db->select('id');
$this->db->from('language');
$this->db->where_in('language', $lang);
$query = $this->db->get();
$l_id =$query->result_array();  

For this query, when I use print_r($l_id) I'm getting an output like this:

Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [id] => 21 ) )  

But I need something like this:

Array ( [0] => 1 [1] => 21 )

Solution

  • Try like this...

    $this->db->select('id');
    $this->db->from('language');
    $this->db->where_in('language',$lang);
    $query = $this->db->get();
    $l_id =$query->result_array();  
    foreach($l_id as $key=>$value)
    {
       $ids[] = $value['id'];
    }
    print_r($ids);
    

    For example:

    $l_id = array(array('id'=>'1'),array('id'=>21));
    //print_r($l_id);
    foreach($l_id as $key=>$value)
    {
        $ids[]=$value['id'];
    }
    
    print_r($ids);