Invalid argument supplied for foreach()
Personally I find this to be the most clean - not sure if it's the most efficient, mind!
if (is_array($values) || is_object($values))
{
foreach ($values as $value)
{
...
}
}
The reason for my preference is it doesn't allocate an empty array when you've got nothing to begin with anyway.
Why Invalid argument supplied for foreach()
Your code and your image do not seem the same.
On your code, just do var_dump($aa)
before foreach($aa AS $key => $val){
.
And check if your variable has array value or not because that error message suggests that the variable $aa
doesn't have any array data.
On your image, change your code variable $id
to $data
on your foreach
because $id
isn't array type. $id
is just variable
.
Warning: Invalid argument supplied for foreach() in... php
You should use is_array
and count
for checking whether a variable is array or not and whether it is an empty array or not.
<?php
$data = $_POST;
if (count($data) > 1)
{
var_dump($data);
foreach ($data as $k => $row)
{
var_dump($row);
if(is_array($row) && count($row)>0)//you should add this.
{
foreach ($row as $k2 => $img)
{
echo $img;
}
}
}
}
Why always Invalid argument supplied for foreach
Change foreach($data as $data){ ....}
to
if ( is_array($data) ) {
foreach($data as $item){
$id_reservasi[] = $item->id_reservasi;
$stok[] = (float) $item->stok;
}
}
or change your function get_chart
to always return array:
function get_chart(){
$hasil = [];
$query = $this->db->query("SELECT id_reservasi,SUM(status_reservasi) AS stok FROM tbl_reservasi GROUP BY id_reservasi");
if($query->num_rows() > 0){
foreach($query->result() as $data){
$hasil[] = $data;
}
}
return $hasil;
}
foreach help
Invalid argument supplied for foreach() : Yii2
Since $plusone_subjects
is not valid json, you can manually change it to valid json and then convert it to an array like so:
$plusone_subjects = "[$plusone_subjects]"; //covert string to valid json;
$plusone_subjects = json_decode($plusone_subjects,true); //covert to array
foreach ($plusone_subjects as $subject)
{
echo $subject["subject_name"];
}
Invalid argument supplied for foreach() in Laravel 8
You can pass data to the view using the with
method.
Try this:
public function index()
{ $posts = Post::all();
return view('posts.index')->with(compact('posts'));
}
Related Topics
What Is the Easiest Way to Check If Time Is Set in Datetime Variable
A Way to Determine Browser Width in PHP Without JavaScript
How to Get Last Insert Id in Sqlite
How to Use Two Foreach to Insert Data in Database
How to Add Dynamic Filter Using PHP MySQL Ajax
Get Selected Option in PHP Without Pressing Submit
How to Update Field to Add Value to Existing Value
Regex for Only Allowing Letters, Numbers, Space, Commas, Periods
How to Install Imagick/Imagemagick PHP Extension on Windows 7
Dompdf Not Rendering Images from the Server But Is Rendering from External Source
Reference - What Does This Symbol Mean in PHP
How to Get Useful Error Messages in PHP
Secure Hash and Salt For PHP Passwords