15

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.

1
  • why not convert it after the result is retrieved ? Commented Aug 3, 2016 at 8:54

7 Answers 7

23

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Only this solution. CI have only 4 functions to return results - result(), result_array(), row(), row_array(); codeigniter.com/user_guide/database/examples.html
21

CodeIgniter's DB implementation doesn't support indexed result arrays, you have to choose between object or associative array.

This is done to make your queries more maintainable, as returning numeric indexes are harder to debug and maintain.

CodeIgniter Docs - database results

but you can do it, i am suggesting very useful inbuilt function array_column() for you

array_column() returns the values from a single column of the input, identified by the column_key. Optionally, an index_key may be provided to index the values in the returned array by the values from the index_key column of the input array.

it will convert your codeigniter's associative array to indexed array.

$sql = "SELECT `id` FROM `loc8_groups`";
$query = $this->db->query($sql);
$array = $query->result_array();
$arr = array_column($array,"id");
print_r($arr);

it will produce array as below:

Array
(
   [0] => 1

   [1] => 2

   [2] => 3

)

Comments

3

use mysql group_concat, to avoid foreach or using array_map,etc.

$sql = "SELECT group_concat(id separator ',') as id FROM `loc8_groups`";
$query = $this->db->query($sql);
$array1 = $query->row_array();
$arr = explode(',',$array1['id']);

print_r($arr);

2 Comments

Researchers, be warned: 1.) GROUP_CONCAT() has a maximum length of 1024. 2.) The default separator is already a comma, so this need not be specified. 3.) This technique would become unreliable if the targetted column might have values containing commas.
row_array will only result the first row
0

Try this code :

 $result=$this->db->select('id')->get('loc8_groups')->result_array();

 $array=array_map (function($value){
                return $value['id'];
            } , $result);

 print_r($array);

1 Comment

This advice was already recommended a year earlier at stackoverflow.com/a/38738889/2943403
0

You can do it like this:

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

$result = array();
$index = 0;
while($row = $query->unbuffered_row('array'))
{
    $result[] = $row['id'];
    $index++;
}

And the result would be like this:

array(100) {
[0]=>
    string(1) "1"
[1]=>
    string(1) "2"
[2]=>
    string(1) "3"
...

Without extra conversions, and i don't see it as a limitation in codeigniter at all, any other framework that provides an extra function for this just does the same under the hood and in codeigniter you can do anything you want if you studied the framework well.

Comments

0

you could do a little something like this:

$hold = ModelName::find('first', array('conditions' => array('register_id = ? AND table_id = ?', $this->register, $table->id)));

Or maybe

$hold = ModelName::find('all', array('conditions' => array('register_id = ? AND table_id = ?', $this->register, $table->id)));

1 Comment

Please provide the link to the CodeIgniter documentation that explains static method find().
0

CodeIgniter doesn't have a dedicated method to isolate a single column from query result sets, but PHP has array_column() which can get a column or property from each row of results.

This is all you need to produce a flat, indexed array of ids.

public function getLoc8Ids(): array
{
    return array_column($this->db->get('loc8_groups')->result(), 'id');
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.