如何返回多个数组?

时间:2015-06-22 13:56:35

标签: 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 = 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;
    }

我想返回$ cal_data,$ cal_data2,$ cal_data3和$ cal_data4中的所有值。 有办法吗?

5 个答案:

答案 0 :(得分:0)

而不是将这种数组规范化为

$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 array_merge($cal_data,$cal_data2,$cal_data3,$cal_data4);

OR

$result['cal_data'] = $cal_data;
$result['cal_data2'] = $cal_data2;
$result['cal_data3'] = $cal_data3;
$result['cal_data4'] = $cal_data4;

return $result;

答案 1 :(得分:0)

来自PHP Manual

  

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

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

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

答案 2 :(得分:0)

做这样的事情:

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

...或者,如果只有四个选项(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;
}

答案 3 :(得分:0)

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

ps:注意[]动态分配。

示例:

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

答案 4 :(得分:0)

如果检查所有条件,则传递所有条件,如果不是elseif

$data['cal_data'] = $cal_data;
$data['cal_data2'] = $cal_data2;
$data['cal_data3'] = $cal_data3;
$data['cal_data4'] = $cal_data4;

return $data;