How to return multiple arrays?

There is the following method in my controller.

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

I want to return all the values ​​in $ cal_data, $ cal_data2, $ cal_data3 and $ cal_data4. Is there any way to do this?

0
source share
5 answers

Instead of making such an array, normalize your array as

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

Edited

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;
0
source

From PHP Manual :

Values ​​are returned using the optional return statement. Any type can be returned, including arrays and objects. This causes the function to immediately stop executing and transfer control back to the line from which it was called. See return for more details .

, , ,

return [ $cal_data, $cal_data2, $cal_data3, $cal_data4 ];
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;
}
0
source

If you have multiple values ​​for the same array index, you can define a dynamic array.

ps: Note the dynamic allocation [].

Example:

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

if you check all conditions then pass all conditions to if if elseif

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

return $data;
Run code
0
source

All Articles