php 数组使未定义索引:0 通知消失

发布于 2024-11-10 10:06:30 字数 420 浏览 2 评论 0原文

是的,所以我打开了 E_NOTICES 并且我的代码可以正常工作,只是每次我尝试使用设置键将数据插入数组时,我都会不断收到“严重性:通知消息:未定义索引:0”。当你尝试调试时,这真的很烦人。

我做错了什么,会导致通知消失而不关闭 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']);
}

Right so i have E_NOTICES on and my code works its just i keep getting "Severity: Notice Message: Undefined index: 0" everytime i try to insert my data into the array with the set key. Its really annoying when ur trying to debug.

What am i doing wrong that will make the notices go away without turning off 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']);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

靑春怀旧 2024-11-17 10:06:30

$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']);
}

Is $teams[$row->home_id] definitely defined?

edit: Quick and dirty test for you:

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']);
}
晌融 2024-11-17 10:06:30

如果没有更多信息,不可能确定,但​​我会检查 $row->position 是否已设置,并且 $row->home_id 是否已设置(如果它们可能未定义)。

Impossible to say for certain without more information, but I would check that $row->position is set and that $row->home_id is set if there is any possibility they may be undefined.

好久不见√ 2024-11-17 10:06:30

您必须先初始化基本数组,然后再将值推入其中。这里的 isset 并没有真正做任何事情。把它扔掉吧。如果仍然出现错误,请确保始终设置 $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']);
}

You must initialize the base array before pushing values to it. The isset here doesn't really do anything. Just throw it away. If you still get the error make sure $teams[$row->home_id]['team_name'] is always set.

$bracketdata = array();
foreach ($bracketmatches->result() as $row)
{
  $bracketdata[$row->position] = array("home_name" => $teams[$row->home_id]['team_name']);
}
千柳 2024-11-17 10:06:30
$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']);
}
$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']);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文