3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Warning: Invalid argument supplied for foreach()の対処法

Posted at

##エラー内容
Invalid argument supplied for foreach() = PHPのforeach文配列データを取り出そうとした場合に、配列のデータが入っていない時に出るエラー。

foreach文配列の要素がある分だけ繰り返し処理をするループ文。

##解決方法
まずは、配列データが入っているかを判定する必要があります。
foreach文で使えるのは配列オブジェクトのみなので、(arry)を使用して変数を強制的に配列にしてあげることで対処できます。

配列とオブジェクトについては別の記事に投稿します。

foreach($stmt as $row){
  //処理内容
}

↓

foreach((array)$stmt as $row){
  //処理内容
}

参考元はこちら
(https://memocarilog.info/wordpress/5132)
(https://qiita.com/takuma-jpn/items/678876ad12b9ae9998ac)

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?