Codeigniter: get all values from single column as array

流过昼夜 提交于 2019-12-01 14:48:07

问题


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:


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);



回答2:


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);



回答3:


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

)



回答4:


I know this question is old, but I wanted to share an easy solution using array_column:

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



回答5:


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);



回答6:


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.




回答7:


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)));



回答8:


Use following script for get result as single associative array that contains all ids

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

print_r($arr);


来源:https://stackoverflow.com/questions/38738693/codeigniter-get-all-values-from-single-column-as-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!