服务器日志中出现错误...我该如何停止它?

发布于 2024-07-15 17:37:38 字数 612 浏览 7 评论 0原文

我收到一个错误,我用谷歌搜索了我能想到的所有内容,几乎没有任何见解。 也许你可以用新的眼光来帮助你。

我有一个返回对象数组的函数。 然后,调用此函数的代码循环遍历此数组并对每个对象执行所需的操作。 当它吐出 HTML 时,它似乎在视觉上工作,但服务器日志仍然给出错误“为 foreach() 提供的参数无效”。 是的,它在技术上可行真是太好了,但我不希望再出现这个错误。 想法?

<?php

// The class whose method returns an array of library_Label objects
class library_Label
{
  public $id = null;
  public $name = null;

  public function getChildren()
  {
    // $children is declared earlier
    return $children;
  }
}


// main code
$columns = new library_Label(1);
foreach ($columns->getChildren() as $label) echo $label->Name;

?>

I'm getting an error and I've googled everything I can think of with barely any insights. Maybe you can help with a fresh set of eyes.

I have a function that returns an array of objects. The code that calls this function then loops through this array and does what it needs to do with each object. It seems to work visually when it spits out the HTML, but the server logs still give an error "Invalid argument supplied for foreach()". Yeah, it's great that it works technically, but I don't want this error to pop up anymore. Thoughts?

<?php

// The class whose method returns an array of library_Label objects
class library_Label
{
  public $id = null;
  public $name = null;

  public function getChildren()
  {
    // $children is declared earlier
    return $children;
  }
}


// main code
$columns = new library_Label(1);
foreach ($columns->getChildren() as $label) echo $label->Name;

?>

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

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

发布评论

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

评论(3

快乐很简单 2024-07-22 17:37:38

这是相当常见的,我的解决方案总是改变这一点:

foreach ($columns->getChildren() as $label) echo $label->Name;

$children = $columns->getChildren();
if (is_array($children)) foreach ($children as $label) echo $label->Name;

应该涵盖所有情况。

您也可以将其扩展为带有大括号的完整 if 语句,然后包含一个 else 块来处理 Children 为空时的情况(如果您愿意)。

This is fairly common, my solution is always to change this:

foreach ($columns->getChildren() as $label) echo $label->Name;

to

$children = $columns->getChildren();
if (is_array($children)) foreach ($children as $label) echo $label->Name;

It should cover all situations.

You can expand it to a full if statement with braces as well and then include an else block to handle the situation when children is empty if you want to.

若能看破又如何 2024-07-22 17:37:38

您没有说明您使用的是什么语言,但这看起来像是 PHP 错误。 如果是这样,请检查 PHP 的错误报告级别。 不过,您实际上应该更改 php.ini 中的值,这样您就不会将其他错误转储到服务器日志中。

当然,无论如何,您都想找出抛出该错误的原因。

You don't say what language you're using, but that looks like a PHP error. If so, check out PHP's error reporting levels. You should probably actually change the values in your php.ini, though, so that you don't dump out other errors into your server logs.

Of course, you'll want to find out why that error is being thrown regardless.

恋你朝朝暮暮 2024-07-22 17:37:38

当您尝试对不是数组的变量执行 foreach 时,会出现警告“为 foreach() 提供的参数无效”。

您需要检查 getChildren() 的返回值实际上是一个数组。

我们无法从您的上下文中判断这一点,因为您既没有提供构造函数源,也没有提供实际创建 $children 的代码。 如果即将到来,可能会更容易帮助您。

您可以尝试的一件事是在执行 foreach 之前使用 "print_r($columns->getChildren())" 打印出返回值。 这应该清楚地表明它是否是一个数组。

我还建议使用 empty()count() 检查数组是否为空 - 网上的一些网站指出空数组也会导致这种情况警告,但我家里没有 PHP 环境来测试它。

The warning "Invalid argument supplied for foreach()" happens when you try to do a foreach on an variable that isn't an array.

You need to check that the return value from getChildren() actually is an array.

We can't tell this from your context since you've provided neither the constructor source nor the code that actually creates $children. If that were forthcoming, it might be easier to help you out.

One thing you could try is printing out the return value before executing the foreach, with "print_r($columns->getChildren())". This should be clear indication as to whether it's an array or not.

I'd also suggest checking to see if the array is empty, using empty() or count() - some sites on the net state that empty arrays will also cause this warning but I don't have a PHP environment at home to test that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文