Codeigniter: get all values from single column as array

iam batman picture iam batman · Aug 3, 2016 · Viewed 20.3k times · Source

Here is my query to get a single column from t

$sql = "SELECT `id` FROM `loc8_groups`";
 $query = $this->db->query($sql);
 print_r($query>result());

Its produce array result like this way.

Array
(
    [0] => stdClass Object
        (
            [id] => 1
        )

    [1] => stdClass Object
        (
            [id] => 2
        )

    [2] => stdClass Object
        (
            [id] => 3
        )

)

But i want result as single associative array that contains all the ids.

Answer

Lakremon picture Lakremon · Aug 3, 2016

Try this code:

$sql = "SELECT `id` FROM `loc8_groups`";
$query = $this->db->query($sql);
$array1=$query>result_array();
$arr = array_map (function($value){
    return $value['id'];
} , $array1);
 print_r($arr);