可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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:
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
)
回答3:
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'));
回答4:
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);
回答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);