我在codeigniter中收到一条错误,说明未定义的偏移:1

时间:2015-05-03 23:34:33

标签: php codeigniter

我的控制器中有这个代码:

$postlist->phaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);
$PhaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);

$postlist->Counter = COUNT($PhaseList);

for($x = 0; $x < COUNT($PhaseList); $x++)
{
    foreach($PhaseList as $row) {
        $TaskID = array( $row['TaskID'] );
        $postlist->taskList = $this->Project->LatestApplicablePlan->WBS->GetWBS($TaskID[$x], $Options);
    }
}

但我收到错误undefined offset : 1。我在数据库中检索的数据不仅仅是单个数据。

1 个答案:

答案 0 :(得分:2)

问题来自这一行:

$TaskID = array( $row['TaskID'] );

这相当于写作:

$TaskID = array(
    0 => $row['TaskID'] 
);

尽管数组TaskID中只有1个元素,但当$x大于零时,会出现错误。

试试这个:

$PhaseList = $this->Project->LatestApplicablePlan->WBS->GetPhaseList($Options);

$postlist->phaseList = $PhaseList;
$postlist->Counter = COUNT($PhaseList);

foreach($PhaseList as $row) {
    $postlist->taskList[] = $this->Project->LatestApplicablePlan->WBS->GetWBS($row['TaskID'], $Options);
}