PHP 警告:为 foreach() 提供的参数无效

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6572061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:45:20  来源:igfitidea点击:

PHP Warning: Invalid argument supplied for foreach()

php

提问by meme

Why am I getting this PHP Warning?

为什么我会收到此 PHP 警告?

Invalid argument supplied for foreach()

foreach()提供的参数无效

Here is my code:

这是我的代码:

// 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

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

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);
}

What causes this warning?

是什么导致此警告?

回答by dkinzer

You should check that what you are passing to foreachis an array by using the is_arrayfunction

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

If you are not sure it's going to be an array you can always check using the following PHP example code:

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

if (is_array($variable)) {

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

回答by Nanne

This means that you are doing a foreach on something that is not an array.

这意味着您正在对不是数组的内容执行 foreach。

Check out all your foreach statements, and look if the thing before the as, to make sure it is actually an array. Use var_dumpto dump it.

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

Then fix the one where it isn't an array.

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

How to reproduce this error:

如何重现此错误:

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

Make sure $skipperis an array.

确保$skipper是一个数组。

回答by Lightness Races in Orbit

Because, on whatever line the error is occurring at (you didn't tell us which that is), you're passing something to foreachthat is not an array.

因为,无论错误发生在哪一行(你没有告诉我们那是哪一行),你传递的foreach不是数组。

Look at what you're passing into foreach, determine what it is (with var_export), find out why it's not an array... and fix it.

看看你传入的foreach是什么,确定它是什么(用var_export),找出为什么它不是数组......并修复它。

Basic, basic debugging.

基本的,基本的调试。

回答by Mr Nobody

Try this.

尝试这个。

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