How to fix the "PHP Warning: foreach() argument must be of type array|object ..." error in PHP

How to fix the "PHP Warning:  foreach() argument must be of type array|object ..." error in PHP

If you are new to PHP Language and currently facing a PHP Warning that says "PHP Warning: foreach() argument must be of type array|object ...", this article can help you to understand about this, why this error occurs, and how to fix this.

Why does "PHP Warning: foreach() argument must be of type array|object" occurs?

The "PHP Warning: foreach() argument must be of type array|object ..." is basically a PHP warning that occurs when the developer is trying to iterate non-object and non-array data using the foreach() function or statement. The said statement allows only iterating an array or object data.

How to Fix the "PHP Warning: foreach() argument must be of type array|object"?

This PHP warning can be easily fixed by checking the data to iterate if it is iterable first before executing the foreach() statement.

Let's assume that we have the following PHP script.

  1. <?php
  2. $array = "test";
  3. foreach($array as $value){
  4.     echo $value."\n";
  5. }
  6. ?>

The script above will result in a PHP Warning like the following image:

How to fix the PHP Warning:  foreach() argument must be of type array|object ... error in PHP

As you can see, the warning occurred because the data value is a string that is not iterable. Check out the following solution for this PHP Warning

Solution

To prevent the error to occur, we must create an If statement that has a condition that checks the data if it is iterable or not. Here's the script that fixes the error that occurred on the script that I provided first.

  1. <?php
  2. $array = "test";
  3. if(is_iterable($array)){
  4.     foreach($array as $value){
  5.         echo $value."\n";
  6.     }
  7. }else{
  8.     print("The data given is not iterable.");
  9. }
  10.  
  11. ?>

The script will result in the following image:

How to fix the PHP Warning:  foreach() argument must be of type array|object ... error in PHP

The is_iterable() is a built-in function of PHP that allows us to check if certain data contains a value that is iterable.

Other Example

Here's another sample that represents the best practice for writing a PHP script using the foreach() function. This will help you to prevent the "PHP Warning: foreach() argument must be of type array|object ..." from occurring.

  1. <?php
  2. $not_iterable = "";
  3. $iterable = ["Foo", "Bar"];
  4. print("Trying to Iterate a Non-iterable data.\n");
  5. if(is_iterable($not_iterable)){
  6.     foreach($not_iterable as $value){
  7.         echo $value."\n";
  8.     }
  9. }else{
  10.     print("\tThe data given is not iterable.\n");
  11. }
  12. print("Trying to Iterate an iterable data.\n");
  13. if(is_iterable($iterable)){
  14.     foreach($iterable as $value){
  15.         echo "\t".$value."\n";
  16.     }
  17. }else{
  18.     print("\tThe data given is not iterable.\n");
  19. }
  20.  
  21. ?>

How to fix the PHP Warning:  foreach() argument must be of type array|object ... error in PHP

There you go! That is how to fix the "PHP Warning: foreach() argument must be of type array|object ..." warning of PHP.

That's it! I hope this tutorial helps you with what you are looking for and fix the problem that you are currently facing on your project.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment