如何从codeigniter一个函数返回多个阵列? [英] How to return multiple arrays from a function in codeigniter?

查看:132
本文介绍了如何从codeigniter一个函数返回多个阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的控制器此功能。

I am having this function in my controller.

function get_data($year, $month) {

        $query = $this->db->select('date,name,type')->from('books')->like('date', "$year-$month", 'after')->get();
        $data = array();
        $data2 = array();
        $data3 = array();


        foreach ($query->result() as $row) {
            if ($row->name == 'AA' && $row->type == 'AM') {
                $data[substr($row->date, 8, 2)] = '<div class="aa_am">' . $row->name . ' ' . $row->type . '</div>';
            } else if ($row->name == 'AA' && $row->type == 'PM') {
                $data2[substr($row->date, 8, 2)] = '<div class="aa_pm">' . $row->name . ' ' . $row->type . '</div>';
            } else if ($row->name == 'BB' && $row->type == 'AM') {
                $data3[substr($row->date, 8, 2)] = '<div class="bb_am">' . $row->name . ' ' . $row->type . '</div>';
            } 
        }
        return $data;
    }

欲检索所有数据是在$数据,$ data1和data2的$同时。是否有可能做到这一点?如果任何人有一个想法,那将是对我帮助。

I want to retrieve all data which is in $data,$data1 and $data2 at the same time. Is it possible to do it?? If anyone have an idea it would be a help for me.

推荐答案

从code,我不知道是什么导致你通过组合数据是在$数据,$ data1和$ data2的期望。

From your code, I don't know what result you expect by combining all data which is in $data, $data1 and $data2.

但你可以尝试 array_merge(数组1,数组2,ARRAY3 ...)的功能,如果你想要的所有数据,其中重点从0开始,并以1为每个值增加。

but you can try array_merge(array1,array2,array3...) function if you want all data where key starting from 0 and increases by 1 for each value.

否则,您可以创建包含所有数据值另一个数组,

Or else you can create another array with all data values like,

返回阵列(数据=&GT; $的数据,数据1=&GT; $数据1,数据2=&GT; $数据2)

通过这种方式可以实现你想要的。

by this way you can achieve what you want.

这篇关于如何从codeigniter一个函数返回多个阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆