简体   繁体   中英

having problems with php syntax and foreach

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

}

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 …) {
        // …
    }
}

您的函数getUserList不返回数组,以确保$ items是这样写的数组:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM