Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何返回多个数组?_Php_Arrays_Codeigniter - Fatal编程技术网

Php 如何返回多个数组?

Php 如何返回多个数组?,php,arrays,codeigniter,Php,Arrays,Codeigniter,我的控制器中有以下方法 function get_calendar_data($year, $month) { $query = $this->db->select('date_cal,title,type')->from('reservations')->like('date', "$year-$month", 'after')->get(); $cal_data = array(); $cal_data2 = a

我的控制器中有以下方法

function get_calendar_data($year, $month) {

        $query = $this->db->select('date_cal,title,type')->from('reservations')->like('date', "$year-$month", 'after')->get();
        $cal_data = array();
        $cal_data2 = array();
        $cal_data3 = array();
        $cal_data4 = array();

        foreach ($query->result() as $row) {
            if ($row->title == 'GK' && $row->type == 'AM') {
                $cal_data[substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'GK' && $row->type == 'PM') {
                $cal_data2[substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'RP' && $row->type == 'AM') {
                $cal_data3[substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
            } else if ($row->title == 'RP' && $row->type == 'PM') {
                $cal_data4[substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
            }
        }

        return $cal_data;
    }
函数获取日历数据($year,$month){
$query=$this->db->select('date_-cal,title,type')->from('reservations')->like('date',“$year-$month','after')->get();
$cal_data=array();
$cal_data2=array();
$cal_data3=数组();
$cal_data4=数组();
foreach($query->result()作为$row){
如果($row->title=='GK'&&$row->type=='AM'){
$cal_data[substr($row->date_-cal,8,2)]='.$row->title'.$row->type'.';
}如果($row->title='GK'&&$row->type=='PM'){
$cal_数据2[substr($row->date_cal,8,2)]='.$row->title'.$row->type'.';
}如果($row->title='RP'&&$row->type='AM'){
$cal_数据3[substr($row->date_cal,8,2)]='.$row->title'.$row->type'.';
}如果($row->title='RP'&&$row->type=='PM'){
$cal_data4[substr($row->date_cal,8,2)]='.$row->title'.$row->type'.';
}
}
返回$cal炪数据;
}
我想返回$CALU data、$CALU data2、$cal\U data3和$cal\U data4中的所有值。
有什么方法可以做到这一点吗???

而不是让这样的数组像

$result = array();

foreach ($query->result() as $row) {
    if ($row->title == 'GK' && $row->type == 'AM') {
        $result['cal_data'][substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
    } else if ($row->title == 'GK' && $row->type == 'PM') {
        $result['cal_data2'][substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
    } else if ($row->title == 'RP' && $row->type == 'AM') {
        $result['cal_data3'][substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
    } else if ($row->title == 'RP' && $row->type == 'PM') {
        $result['cal_data4'][substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
    }
}

return $result;

发件人:

使用可选的return语句返回值。可以返回任何类型,包括数组和对象。这会导致函数立即结束其执行,并将控制权传递回调用它的行。有关更多信息,请参阅

但是,您可以返回一个包含所有数组的数组

return [ $cal_data, $cal_data2, $cal_data3, $cal_data4 ];

这样做:

function get_calendar_data($year, $month) {

    $query = $this->db->select('date_cal,title,type')->from('reservations')->like('date', "$year-$month", 'after')->get();
    $cal_data = array(
        "GK_AM" => array(),
        "GK_PM" => array(),
        "RP_AM" => array(),
        "RP_PM" => array(),
    );

    foreach ($query->result() as $row) {
        if ($row->title == 'GK' && $row->type == 'AM') {
            $cal_data['GK_AM'][substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'GK' && $row->type == 'PM') {
            $cal_data['GK_PM'][substr($row->date_cal, 8, 2)] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'RP' && $row->type == 'AM') {
            $cal_data['RP_AM'][substr($row->date_cal, 8, 2)] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'RP' && $row->type == 'PM') {
            $cal_data['RP_PM'][substr($row->date_cal, 8, 2)] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
        }
    }

    return $cal_data;
}
函数获取日历数据($year,$month){
$query=$this->db->select('date_-cal,title,type')->from('reservations')->like('date',“$year-$month','after')->get();
$cal_数据=数组(
“GK_AM”=>数组(),
“GK_PM”=>数组(),
“RP_AM”=>数组(),
“RP_PM”=>数组(),
);
foreach($query->result()作为$row){
如果($row->title=='GK'&&$row->type=='AM'){
$calu数据['GK_AM'][substr($row->date_-cal,8,2)]='.$row->title.'.$row->type.';
}如果($row->title='GK'&&$row->type=='PM'){
$cal_数据['GK_PM'][substr($row->date_-cal,8,2)]='.$row->title.'.$row->type'.';
}如果($row->title='RP'&&$row->type='AM'){
$cal_data['RP_AM'][substr($row->date_-cal,8,2)]='.$row->title'.$row->type'.';
}如果($row->title='RP'&&$row->type=='PM'){
$cal_data['RP_PM'][substr($row->date_-cal,8,2)]='.$row->title'.$row->type'.';
}
}
返回$calu数据;
}
…或者,如果只有四个选项(GKAM、GKPM、RPAM、RPPM),那么这个选项甚至更短

function get_calendar_data($year, $month) {

    $query = $this->db->select('date_cal,title,type')->from('reservations')->like('date', "$year-$month", 'after')->get();
    $cal_data = array(
        "GK_AM" => array(),
        "GK_PM" => array(),
        "RP_AM" => array(),
        "RP_PM" => array(),
    );

    foreach ($query->result() as $row) {
        $cal_data[$row->title. '_' . $row->type][substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
    }

    return $cal_data;
}
函数获取日历数据($year,$month){
$query=$this->db->select('date_-cal,title,type')->from('reservations')->like('date',“$year-$month','after')->get();
$cal_数据=数组(
“GK_AM”=>数组(),
“GK_PM”=>数组(),
“RP_AM”=>数组(),
“RP_PM”=>数组(),
);
foreach($query->result()作为$row){
$cal_数据[$row->title.'.$row->type][substr($row->date_-cal,8,2)]='.$row->title.'.$row->type'.';
}
返回$calu数据;
}

如果同一数组索引有多个值,则可以定义动态数组

注意[]动态分配

例如:

    foreach ($query->result() as $row) {
        if ($row->title == 'GK' && $row->type == 'AM') {
            $cal_data[substr($row->date_cal, 8, 2)][] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'GK' && $row->type == 'PM') {
            $cal_data2[substr($row->date_cal, 8, 2)][] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'RP' && $row->type == 'AM') {
            $cal_data3[substr($row->date_cal, 8, 2)][] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'RP' && $row->type == 'PM') {
            $cal_data4[substr($row->date_cal, 8, 2)][] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
        }
    }

    return $cal_data;
}
foreach($query->result()作为$row){
如果($row->title=='GK'&&$row->type=='AM'){
$cal_数据[substr($row->date_cal,8,2)][]='.$row->title'.$row->type'.';
}如果($row->title='GK'&&$row->type=='PM'){
$cal_数据2[substr($row->date_cal,8,2)][]='.$row->title'.$row->type'.';
}如果($row->title='RP'&&$row->type='AM'){
$cal_数据3[substr($row->date_cal,8,2)][]='.$row->title'.$row->type'.';
}如果($row->title='RP'&&$row->type=='PM'){
$cal_数据4[substr($row->date_cal,8,2)][]='.$row->title'.$row->type'.';
}
}
返回$calu数据;
}

如果您选中所有条件,则在“如果不是”elseif中传递所有条件
$data['cal\u data']=$cal\u data;
$data['CALU data2']=$CALU data2;
$data['CALU data3']=$CALU data3;
$data['CALU data4']=$CALU data4;

返回$data将所有内容放在一个数组中并返回该数组。您的问题已得到回答,或者您问错了。请接受最能回答您问题的答案。@AndreschSerj这些方法中的任何一种都不适合我。@IshaniPathinayake:那么您应该重写您的问题。尽量弄清楚你想要的输出是什么。所有答案以不同的方式返回$CALU data、$CALU data2、$CALU data3和$CALU data4中的所有th值。要清楚你的期望是什么,给出的答案中有什么是从中得出的。我试过这个。这对meDid不起作用,你有任何错误。或者试试我编辑过的答案,我认为这应该行得通。但是它没有给我预期的输出,因为我检查条件的方式是错误的。这里,如果第一个条件为真,则不会检查其他条件。我要检查所有的条件。有什么办法吗?你可以用
开关箱
这对我不起作用:(@IshaniPathinayake,你得到了什么错误或行为?对于我以前的代码,我只得到一个数组数据。当我使用你的代码时,我没有得到任何数据。
$cal_data
显然正在加载和返回。你用什么方法测试数组中是否有数据?请显示代码。请参阅此链接。这是我的完整代码
function get_calendar_data($year, $month) {

    $query = $this->db->select('date_cal,title,type')->from('reservations')->like('date', "$year-$month", 'after')->get();
    $cal_data = array(
        "GK_AM" => array(),
        "GK_PM" => array(),
        "RP_AM" => array(),
        "RP_PM" => array(),
    );

    foreach ($query->result() as $row) {
        $cal_data[$row->title. '_' . $row->type][substr($row->date_cal, 8, 2)] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
    }

    return $cal_data;
}
    foreach ($query->result() as $row) {
        if ($row->title == 'GK' && $row->type == 'AM') {
            $cal_data[substr($row->date_cal, 8, 2)][] = '<div class="gk_am">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'GK' && $row->type == 'PM') {
            $cal_data2[substr($row->date_cal, 8, 2)][] = '<div class="gk_pm">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'RP' && $row->type == 'AM') {
            $cal_data3[substr($row->date_cal, 8, 2)][] = '<div class="rp_am">' . $row->title . ' ' . $row->type . '</div>';
        } else if ($row->title == 'RP' && $row->type == 'PM') {
            $cal_data4[substr($row->date_cal, 8, 2)][] = '<div class="rp_pm">' . $row->title . ' ' . $row->type . '</div>';
        }
    }

    return $cal_data;
}