Actually this error is coming from a foreach loop. I just try to var_dump
values in an array using this foreach loop.
foreach [$items as $item] {
// ...
}
In the PHP manual, it says that
The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialised variable.
This is the basic cause behind this warning. We are passing something to foreach loop that is not an array.
To solve this warning, we have two simple options at here.
Solution 1
We can check the type of that variable & if it is an array, then only pass it to the foreach loop.
if [is_array[$items]] {
foreach [$items as $item] {
// ...
}
}
Now we ensure that we have an array for foreach loop.
Solution 2
We can also solve this warning by cast that variable to an array in the loop. This is a lot cleaner, requires less typing and only needs one edit on a single line.
các bạn cho mình hỏi mình có xây dựng kết nối csdl bang class nhung khi minh dùng foreach để hiện hiện thị dữ liệu ra thì nó báo lỗi này
Warning: Invalid argument supplied for foreach[] in C:\xampp\htdocs\suavinamilk\loaisua.php on line 24
còn đây là file của mình
Database.php
xl_loai_sua.php
loaisua.php
danh muc loai sua |