简体   繁体   English

PHP:未定义的偏移量错误

[英]PHP: Undefined offset error

The below-provided code is used to fill an array "data". 以下提供的代码用于填充数组“数据”。

$query1="SELECT * FROM tab1, tab2 WHERE tab1.column1=tab2.column2;";
    $result1=DatabaseConnector::ExecuteQueryArray($query1);
    $data = array();
    $i = 0;
    foreach ($result1 as $row):
        $data[] = array(
            array($i,array("xxx",' EE112',$row['column3'],'FT445'),"2004-03-01 10:00","2004-03-01 14:00"));
        $i++;
    endforeach;

When I try reading data from the array, the error "Undefined offset: 1" occurs. 当我尝试从数组中读取数据时,会出现错误“Undefined offset:1”。 The funny thing is that when I filled "data" array using $data = and not $data[] = , there was no error, just the last row was filled. 有趣的是,当我使用$data =而不是$data[] =填充“data”数组时,没有错误,只填充了最后一行。 The error is produced by line $bar = new GanttBar(..) . 错误由行$bar = new GanttBar(..) I tried to substitute $row['column3'] by some string "xxx", but there was the same error. 我试图用一些字符串“xxx”替换$row['column3'] ,但是出现了同样的错误。

for($i=0; $i<count($data); ++$i) {
        $bar = new GanttBar($data[$i][0],$data[$i][1],$data[$i][2],$data[$i][3]);
        $graph->Add($bar);
    }

Isn't 是不是

$data[] = array("xxx",' EE112',$row['column3'],'FT445',"2004-03-01 10:00","2004-03-01 14:00");

just enough, data structure should be as simple as possible. 恰到好处,数据结构应该尽可能简单。

Ok, if you want that structure. 好的,如果你想要那个结构。

$data[] = array(array("xxx",' EE112',$row['column3'],'FT445'),"2004-03-01 10:00","2004-03-01 14:00");

Then 然后

foreach($data as $i => $var) {
    $bar = new GanttBar($i, $var[0], $var[1], $var[2]);
    $graph->Add($bar);
}

I think there is a very basic problem: 我认为有一个非常基本的问题:

The foreach loop is creating a new subarray in $data for every row. foreach循环在每行的$ data中创建一个新的子数组。 But later on your code is trying to get three rows from each entry in $data - which never has been created. 但是后来您的代码试图从$ data中的每个条目中获取三行 - 这些行从未创建过。 Therefore $data[$i][1] is always empty. 因此$data[$i][1]总是空的。

You might need to adjust your sql query to receive the data in the correct format from the beginning. 您可能需要调整sql查询以从头开始接收正确格式的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM