简体   繁体   中英

foreach return single result codeigniter

I want a array with owen key but when i am trying to do this it return only first result any idea?

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

This should be like,

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

Try this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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