How to return multiple arrays from a function in codeigniter?

前端 未结 2 429
失恋的感觉
失恋的感觉 2021-01-14 16:04

I am having this function in my controller.

function get_data($year, $month) {

        $query = $this->db->select(\'date,name,type\')->from(\'books         


        
2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 16:31

    You can do it in following way,

    function get_data($year, $month) {
    
        $query = $this->db->select('date,name,type')->from('books')->like('date', "$year-$month", 'after')->get();
        $data = array();
        $data1 = array();
        $data2 = array();
    
    
        foreach ($query->result() as $row) {
            if ($row->name == 'AA' && $row->type == 'AM') {
                $data[substr($row->date, 8, 2)] = '
    ' . $row->name . ' ' . $row->type . '
    '; } else if ($row->name == 'AA' && $row->type == 'PM') { $data1[substr($row->date, 8, 2)] = '
    ' . $row->name . ' ' . $row->type . '
    '; } else if ($row->name == 'BB' && $row->type == 'AM') { $data2[substr($row->date, 8, 2)] = '
    ' . $row->name . ' ' . $row->type . '
    '; } } $return['data']=$data; $return['data1']=$data1; $return['data2']=$data2; return $data; }

    you can acces in view as varible,

    $data['index'], $data1['index'], $data2['index']
    

    if you want to merge them then,

    array_merge($data,$data1,$data2);
    

提交回复
热议问题