foreach返回单个结果codeigniter

时间:2017-03-29 08:30:53

标签: php codeigniter

我想要一个带有owen键的数组 但是当我试图这样做时,它只返回第一个结果吗?

    $query = $this->db->get('users');
    if ($query->num_rows() > 0) {
    foreach($query->result() as $row) {
        $data = array(
         'Name'-> $row->name
        );
    }
    $users = $data;

2 个答案:

答案 0 :(得分:1)

这应该是,

$data = array();
    $query = $this->db->get('users');
    if ($query->num_rows() > 0) {
    foreach($query->result() as $row) {
        $data[] = $row->name;        
    }
print_r($data);
    $users = $data;

答案 1 :(得分:0)

试试这个:

  $data = array();
  foreach($query->result() as $row) {
        array_push($data,array(
         'Name'-> $row->name
        ));
    }
    $users = $data;