having problems with php syntax and foreach
getting an Warning: Invalid argument su开发者_如何学JAVApplied 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
}
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 …) {
// …
}
}
your function getUserList does not returning array to make sure that $items is array write like this:
$items = (array) getUserList($user,0,100);
精彩评论