尝试创建数组但创建一个对象

时间:2015-05-19 15:40:58

标签: php arrays object

我目前正在尝试在php中创建一个具有当前格式的数组: calendar [month] [row] [column],其中month = 1到6

这基本上就是我用来创建它的代码。还有更多,但我希望这就足够了。

function db_to_matrix($date_start, $date_end, $compliment, $workers, $worker_shifts, $shift_types)
{
    $calendar = array();
    //Sets the titles up for each months array
    $date = clone $date_start;
    while ($date <= $date_end)
    {
        $calendar[date_format($date,'n')] = array();
        $calendar[date_format($date,'n')][0][] = "db_table";
        $calendar[date_format($date,'n')][0][] = "id";
        $calendar[date_format($date,'n')][0][] = "";
        $calendar[date_format($date,'n')][1][] = "";
        $calendar[date_format($date,'n')][1][] = "";
        $calendar[date_format($date,'n')][1][] = "";
        $calendar[date_format($date,'n')][2][] = "compliment";
        $calendar[date_format($date,'n')][2][] = "compliment_day";
        $calendar[date_format($date,'n')][2][] = "Day Comp";
        $calendar[date_format($date,'n')][3][] = "compliment";
        $calendar[date_format($date,'n')][3][] = "compliment_day_actual";
        $calendar[date_format($date,'n')][3][] = "Act Day";
        $calendar[date_format($date,'n')][4][] = "compliment";
        $calendar[date_format($date,'n')][4][] = "compliment_night";
        $calendar[date_format($date,'n')][4][] = "Night Comp";
        $calendar[date_format($date,'n')][5][] = "compliment";
        $calendar[date_format($date,'n')][5][] = "compliment_night_actual";
        $calendar[date_format($date,'n')][5][] = "Act Night";
        date_modify($date,"+1 month");
    }
}

我最终得到的是:

Object {1:Array [95],2:Object,3:Object,4:Object,5:Object,6:Object}

我很好用$ calendar作为对象,但之后的每个“变量”我想得到Array。示例:Object {1:Array [95],2:Array [95],3:Array [95],4:Array [95],5:Array [95],6:Array [95]}

1 个答案:

答案 0 :(得分:1)

您可能正在重复使用变量,尝试使用其他变量名称或在while循环之前将$ calendar重置为数组。

$calendar = array();