【问题标题】:PHP Warning: Invalid argument supplied for foreach()PHP 警告:为 foreach() 提供的参数无效
【发布时间】:2011-07-04 13:16:37
【问题描述】:

为什么我会收到这个 PHP 警告?

foreach()

提供的参数无效

这是我的代码:

// look for text file for this keyword
if (empty($site["textdirectory"])) {
    $site["textdirectory"] = "text";
}
if (file_exists(ROOT_DIR.$site["textdirectory"].'/'.urlencode($q).'.txt')) {
    $keywordtext = 
     file_get_contents(ROOT_DIR.$site["textdirectory"].'/'.urlencode($q).'.txt');
}
else {
    $keywordtext = null;
}

$keywordsXML = getEbayKeywords($q);

foreach($keywordsXML->PopularSearchResult as $item) {
    $topicsString = $item->AlternativeSearches;
   $relatedString = $item->RelatedSearches;
   if (!empty($topicsString)) {
        $topics =  split(";",$topicsString);
    }
    if (!empty($relatedString)) {
        $related = split(";",$relatedString);
    }

}

$node = array();
$node['keywords'] = $q;

2

$xml = ebay_rss($node);

$ebayItems = array();
$totalItems = count($xml->channel->item);

$totalPages = $totalItems / $pageSize;


$i = 0;
foreach  ($xml->channel->item as $item) {
  $ebayRss = 
    $item->children('http://www.ebay.com/marketplace/search/v1/services');

    if ($i>=($pageSize*($page-1)) && $i<($pageSize*$page)) {
        $newItem = array();
        $newItem['title'] = $item->title;
        $newItem['link'] = buyLink($item->link, $q);
        $newItem['image'] = ebay_stripImage($item->description);
        $newItem['currentbid'] = ebay_convertPrice($item->description);
        $newItem['bidcount'] = $ebayRss->BidCount;
        $newItem['endtime'] = ebay_convertTime($ebayRss->ListingEndTime);
        $newItem['type'] = $ebayRss->ListingType;
        if (!empty($ebayRss->BuyItNowPrice)) {
            $newItem['bin'] = ebay_convertPrice($item->description);
        }
        array_push($ebayItems, $newItem);
    }
    $i++;
}


$pageNumbers = array();
for ($i=1; $i<=$totalPages; $i++) {
    array_push($pageNumbers, $i);
}

3

// get user guides
$guidesXML = getEbayGuides($q);
$guides = array();
foreach ($guidesXML->guide as $guideXML) {
    $guide = array();
    $guide['url'] = makeguideLink($guideXML->url, $q);
    $guide['title'] = $guideXML->title;
    $guide['desc'] = $guideXML->desc;
    array_push($guides,$guide);
}

是什么导致了这个警告?

【问题讨论】:

  • 查看编辑屏幕上的{} 按钮:它会像我现在所做的那样为您格式化代码;)
  • 因为您向 foreach 提供了无效参数。你检查过你输入的什么吗? var_dump可以帮到你。
  • foreach 中的哪一个会产生错误?你的错误信息应该给你一个行号...
  • 它有一个 PHP 警告:为所有三个提供的参数无效: foreach($keywordsXML->PopularSearchResult as $item) { foreach ($xml->channel->item as $item) { foreach ( $guidesXML->guide as $guideXML) {

标签: php


【解决方案1】:

您应该使用 is_array 函数检查您传递给 foreach 的内容是一个数组

如果您不确定它是否是一个数组,您可以随时使用以下 PHP 示例代码进行检查:

if (is_array($variable)) {

  foreach ($variable as $item) {
   //do something
  }
}

【讨论】:

  • 为什么,我在问什么会导致我得到。为什么我收到 PHP 警告:在我的脚本中的三个 foreach 上为 foreach() 提供的参数无效。
  • 在大多数情况下这会起作用,但并不总是看到:stackoverflow.com/a/2630032/560287
  • 优秀。我在解析 RSS 提要的自定义 Wordpress 插件时遇到问题。默认情况下,此提要并不总是有内容,并且会在有更多内容之前过期。所以数组并不总是被填充。这解决了我在那种情况下收到的“偏移”警告。
【解决方案2】:

这意味着您正在对不是数组的东西进行 foreach。

检查所有的 foreach 语句,并查看 as 之前的内容,以确保它实际上是一个数组。使用var_dump 转储它。

然后修复它不是数组的那个。

如何重现此错误:

<?php
$skipper = "abcd";
foreach ($skipper as $item){       //the warning happens on this line.
    print "ok";
}
?>

确保$skipper 是一个数组。

【讨论】:

    【解决方案3】:

    因为,无论错误发生在哪一行(您没有告诉我们是哪一行),您都在向foreach 传递一些不是数组的东西。

    查看您传递给foreach 的内容,确定它是什么(使用var_export),找出它为什么不是数组...并修复它。

    基本的,基本的调试。

    【讨论】:

      【解决方案4】:

      试试这个。

      if(is_array($value) || is_object($value)){
          foreach($value as $item){
           //somecode
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-01
        • 1970-01-01
        相关资源
        最近更新 更多