PHP 2d数组中未定义的偏移量错误

PHP 2d数组中未定义的偏移量错误,php,multidimensional-array,Php,Multidimensional Array,编辑:代码可在此处查看: 我已经找过了,但似乎没有什么能比得上我的问题 在我开始之前:我已经把所有可能是问题的代码放在下面。这是大约200行代码,我非常抱歉!但我完全被难住了 我有一个2D数组,$val,它由一个函数填充 然后将数组传递给另一个函数,该函数使用数据填充表 当我把一个函数一分为二时,问题才出现 我有一个函数,它将获得一个数据负载,用它填充2D数组,然后为该数据创建显示。我决定拆分它-一个函数填充数组,另一个函数以所需格式显示此数据 但是,由于将函数一分为二,即使仔细分析了代码,由于

编辑:代码可在此处查看:

我已经找过了,但似乎没有什么能比得上我的问题

在我开始之前:我已经把所有可能是问题的代码放在下面。这是大约200行代码,我非常抱歉!但我完全被难住了

我有一个2D数组,
$val
,它由一个函数填充

然后将数组传递给另一个函数,该函数使用数据填充表

当我把一个函数一分为二时,问题才出现

我有一个函数,它将获得一个数据负载,用它填充2D数组,然后为该数据创建显示。我决定拆分它-一个函数填充数组,另一个函数以所需格式显示此数据

但是,由于将函数一分为二,即使仔细分析了代码,由于某些原因,数组要么填充不正确,要么读取不正确,要么出现了其他问题

包括我的代码转储,因为我自己的眼睛显然无法发现问题

对不起,代码太多了。但我已经检查了每一行,无法找出哪里出了问题——特别是当它是一个函数时,它工作得很好

未定义的偏移误差在以下代码中指示的点处循环

全局变量和第一个函数-从CSV中获取数据

/*
  variables that work out current week
*/

$termstart = strtotime("03 October 2011"); //start of term, set manually every year, week 1 is first week after freshers.
$todaysdate = strtotime("now"); //todays date
$weekdif = ceil(($todaysdate-$termstart)/604800);//weeks between the two dates
//define global variables and arrays
$row = 0;
$col = 0;
$num = 0;


//this is the main timetable interface array
$val = array(array());

$ttdata = array();

$lecs = array();

tableinit($weekdif);


function tableinit($wkd)
{

$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
$times = array("09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00");


//Check for appropriate CSV file and open it
if (($handle = fopen("timetable.csv", "r")) !== FALSE)
{
    //Check file for data and copy it into an array
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
        //filter out blank lines in the file
        $fdata = array_filter($data);
        $num = count($fdata);

        //If line is not empty
        if ($num > 0)
        {
            //for every value in the array (the line)
            for ($c=0; $c<$num; $c++)
            {

                /*
                  This gets the module code, trims it of useless data,
                  then adds the name into an array of lectures for comparison later.
                  This is used to set up different colours for each different module
                */

                if ($c == 3)
                {
                    $lecture = substr($fdata[$c],0,8);

                    if (empty($lecs))
                    {
                        $lecs[] = $lecture;
                    }
                    else if (!(in_array($lecture,$lecs)))
                    {
                        $lecs[] = $lecture;
                    }

                    $ttdata[] = $lecture;
                }

                //if it's the 4th value or higher, then its data we want to display.
                if ($c >= 4)
                {
                    //add the data to an array. If no array exists, create it
                    $ttdata[] = $fdata[$c];
                }



                /*
                  if the value is a day of the week
                  set the value of the first timetable column as the appropriate day
                  with the corresponding row
                */

                switch ($fdata[$c])
                {
                    case $days[0]:
                        $row = 0;
                        $val[$row][0] = $fdata[$c];
                        break;
                    case $days[1]:
                        $row = 1;
                        $val[$row][0] = $fdata[$c];
                        break;
                    case $days[2]:
                        $row = 2;
                        $val[$row][0] = $fdata[$c];
                        break;
                    case $days[3]:
                        $row = 3;
                        $val[$row][0] = $fdata[$c];
                        break;
                    case $days[4]:
                        $row = 4;
                        $val[$row][0] = $fdata[$c];
                        break;
                    case $days[5]:
                        $row = 5;
                        $val[$row][0] = $fdata[$c];
                        break;
                    case $days[6]:
                        $row = 6;
                        $val[$row][0] = $fdata[$c];
                        break;
                }

                /*
                  this function compares the current week to the weeks in the timetable.
                  If there's a match, add a flag to the array for that lecture.
                  if not, do nothing.
                */

                if ($c == 6)
                {
                    $exp1 = explode(",", $fdata[$c]);

                    foreach ($exp1 as $i)
                    {
                        $i = trim($i);
                        $exp2 = explode("-", $i);

                        if (($wkd >= $exp2[0])&&($wkd <= $exp2[1]))
                        {
                            $ttdata[] = TRUE;
                        }
                    }
                }

                /*
                  if c the second value in the array,
                  check the value against the Time array
                  and set the column appropriately
                */
                if ($c==1)
                {
                    switch ($fdata[$c])
                    {
                        case $times[0]:
                            $col = 1;
                            break;
                        case $times[1]:
                            $col = 2;
                            break;
                        case $times[2]:
                            $col = 3;
                            break;
                        case $times[3]:
                            $col = 4;
                            break;
                        case $times[4]:
                            $col = 5;
                            break;
                        case $times[5]:
                            $col = 6;
                            break;
                        case $times[6]:
                            $col = 7;
                            break;
                        case $times[7]:
                            $col = 8;
                            break;
                        case $times[8]:
                            $col = 9;
                            break;
                    }
                }
            } //end line

            //fill the timetable with whitespace to preserve shape and empty slots
            for ($i=0;$i<=6;$i++)
            {
                for ($j=1;$j<=9;$j++)
                {
                    if (!isset($val[$i][$j]))
                    {
                        $val[$i][$j] = "&nbsp;";
                    }
                }
            }

            //if there's a flag to display data
            if (isset($ttdata[4]))
            {
                //remove the flag
                unset($ttdata[4]);
                //fill the current timetable position with the array of data to display
                $val[$row][$col] = $ttdata;
            }

            //delete the array of data to display
            unset($ttdata);
        }
    }
    fclose($handle);//close the file when finished
}

}
/*
计算当前周的变量
*/
$termstart=STROTIME(2011年10月3日)//学期开始,每年手动设置,第一周是新生入学后的第一周。
$todaysdate=标准时间(“现在”)//今天的日期
$weekdif=ceil($todaysdate-$termstart)/604800)//两个日期之间的星期
//定义全局变量和数组
$row=0;
$col=0;
$num=0;
//这是主时间表接口阵列
$val=array(array());
$ttdata=array();
$lecs=array();
tableinit($weekdif);
函数tableinit($wkd)
{
$days=数组(“周一”、“周二”、“周三”、“周四”、“周五”、“周六”、“周日”);
$times=数组(“09:00”、“10:00”、“11:00”、“12:00”、“13:00”、“14:00”、“15:00”、“16:00”、“17:00”);
//检查相应的CSV文件并打开它
if($handle=fopen(“timeline.csv”,“r”)!==FALSE)
{
//检查文件中的数据并将其复制到数组中
while(($data=fgetcsv($handle,1000,“,”)!==FALSE)
{
//过滤掉文件中的空行
$fdata=阵列过滤器($data);
$num=计数($fdata);
//如果行不是空的
如果($num>0)
{
//对于数组(行)中的每个值

对于($c=0;$c=$exp2[0])&($wkd您需要使用
global
关键字为
tableinit
提供对
$val
变量的访问权限:

  function tableinit($wkd) {
        global $val;

请参阅PHP关于变量作用域的手册页面:。如果没有
global
关键字,您的函数将创建一个本地版本的
$val
,并对其进行操作,但是您在页面顶部定义的
$val
保持不变。

此代码是否构成了整个tt.PHP,因为我没有319行?您应该将在一些代码板上,比如说,没有。上面有23行我开始引用我的代码,其中设置了页面doctype,在jquery和我自己的js文件中导入,设置了css,等等。但是php代码都在那里。我记下了我大量代码片段中出现的两行错误(如
//LOOP is HERE
)你能添加完整的代码吗?我真的不知道。谢谢!现在我只需要全局处理一些其他变量。=)@omicronlyrae:
全局
只会移除症状,但不会治愈原因。相反,开始将代码分组到函数中,并通过参数传递值。如果参数变得复杂,请使用结构。
  function tableinit($wkd) {
        global $val;