0

我是 PHP 新手。我编写了下面给出的代码来创建数组“数据”。它应该有 10 行,但由于某种原因,只有最后(第 10)行被填充。SQL 查询肯定没问题,因为我在 MySQL 查询管理器中检查了它(查询返回 10 行)。

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

更新 1:我有另一个与我最初的问题有关的问题。当我尝试从数组中读取数据时,出现错误“未定义的偏移量:1”。有趣的是,当我使用$data =and not填充“数据”数组$data[] =时,没有错误,只是填充了最后一行。

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

3 回答 3

2

那是因为您在每次迭代时都覆盖了整个 $data 变量。利用

$data[] = array(...

至于最新的错误 - 你应该foreach用来迭代一个数组。这就是它的目的。

于 2012-05-17T08:22:53.063 回答
2

应该喜欢这个附加到一个数组。

$data[] = array($i,array("xxx",' EE112',$row['column3'],'FT445'),"2004-03-01 10:00","2004-03-01 14:00");
于 2012-05-17T08:24:11.833 回答
1
$data = array(...); // Typecasts $data to a new array with given keys and values
$data[] = array(...); // Typecasts a new array to the next array pointer in $data

上面,第 1 行将创建一个全新的数组,用于$data覆盖所有以前的数据。在第 2 行,新数组将存储在已经存在的数组$data中。

于 2012-05-17T08:28:08.363 回答