php数组制作Undefined索引:0通知消失

时间:2011-05-28 18:30:45

标签: php arrays e-notices

对,所以我有E_NOTICES并且我的代码正常工作它只是我不断得到“严重性:通知消息:未定义索引:0”每次我尝试使用set键将数据插入到数组中。当你试图调试它真的很烦人。

我做错了什么会让通知在不关闭E_NOTICES的情况下消失?

foreach ($bracketmatches->result() as $row)
{
  if(!isset($bracketdata[$row->position])) 
  {
    $bracketdata[$row->position] = array(); 
  }
  $bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}

4 个答案:

答案 0 :(得分:2)

$teams[$row->home_id]是否明确定义?

编辑:快速而肮脏的测试:

foreach ($bracketmatches->result() as $row)
{
  if(!isset($teams[$row->home_id]))
  {
    die('GOTCHA!!!');
  }
  $bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}

答案 1 :(得分:1)

在没有更多信息的情况下无法确定,但我会检查$ row->位置是否已设置,如果有可能未定义$ row-> home_id,则会设置。

答案 2 :(得分:0)

在向其推送值之前,必须初始化基础数组。这里的确实并没有真正做任何事情。把它扔掉如果仍然出现错误,请确保始终设置$teams[$row->home_id]['team_name']

$bracketdata = array();
foreach ($bracketmatches->result() as $row)
{
  $bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}

答案 3 :(得分:0)

$bracketdata = array();
foreach ($bracketmatches->result() as $row)
{
  if (isset($row->position) && !empty($row->position) && isset($teams[$row->home_id]['team_name']))
     $bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}