0
votes

getting an Warning: Invalid argument supplied for foreach() in /home/maxer/domains/x/public_html/x/items.php on line 41

line 41 is the foreach

$items = getUserList($user,0,100);

foreach($items as $item){

    echo "<img src=\"".$item['image']."\">"; //image
    echo ""; //title
    echo ""; //button for add to list

}
2
what does vardump($items) say? - Tordek
As an aside, one thing that's kind of nice to do when writing a function that might return an array is to make it return an empty array if there are no items found. That way, you never run into this error. - davidtbernal
I switched to this method as well as casting - chris

2 Answers

3
votes

your function getUserList does not returning array to make sure that $items is array write like this:

$items = (array) getUserList($user,0,100);
4
votes

That means $items is not an array or doesn't implement Traversable. If you supply something that's not an array and doesn't implement Traversable to foreach, it'll complain with this message. Either cast the result of getUserList to an array or check to see if it is one.

$items = (array)getUserList($user,0,100);

or something like this:

$items = getUserList($user,0,100);

if (!is_array($items)) {
    // error
} else {
    foreach ($items …) {
        // …
    }
}