简体   繁体   English

从codeigniter中的result_array获取spesified列

[英]Get spesified column from result_array in codeigniter

I'm about using CodeIgniter for my first project. 我打算在第一个项目中使用CodeIgniter。 I have tried to get data from database. 我试图从数据库中获取数据。

$test = $this->komponen_model->get_participants_id()->result_array();
print_r($test)
Array ( 
   [0] => Array ( [id] => 1 ) 
   [1] => Array ( [id] => 4 ) 
   [2] => Array ( [id] => 7 ) 
) 

And I got this. 我知道了。 So, I need to call each element by: 因此,我需要通过以下方式调用每个元素:

foreach ($test as $ts){
   echo $ts['id'];
}

My question is, can I make the array shorten. 我的问题是,我可以使数组缩短吗? Just like: 就像:

Array ( 
   [0] => 1  
   [1] => 4  
   [2] => 7  
) 

I will be appreciated for anyone's advise. 如有任何建议,我将不胜感激。

Use array_column 使用array_column

$test = array_column($test,'id');

foreach ($test as $ts){
   echo $ts;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM