. Advertisement .
..3..
. Advertisement .
..4..
This tutorial shall guide you thoroughly how to fix the error Warning: Invalid argument supplied for foreach() in […]. Wondering no more but scroll down and rack up the most approachable techniques!
What Causes The Warning: Invalid argument supplied for foreach() in […] Error?
Every PHP developer will encounter the following error at some time or another.
As such, an error will occur once your foreach expression tries to loop over a variable that isn’t an array. A failed SQL query or a function that returns FALSE when there are no results is typically to blame.
It can also occur when your code only creates the array when a specific IF statement evaluates to TRUE.
Let’s imagine, for illustration purposes, that we have a method named get user posts.
This function’s output will be an array of user comments. However, the method shall return a boolean result of FALSE if there are no comments.
Check out the following example to grasp more!
//Get user's posts.
$names = get_user_posts($userId);
//Loop through the $names array.
foreach($names as $userPost){
//Do something.
}
As you can see in the above code snippet, our code incorrectly believes that the $posts variable will always be an array.
Our foreach loop won’t operate if the get user posts function returns a boolean value of FALSE. That is why PHP shall have no way but generate the following error as an output.
Warning: Invalid argument supplied for foreach() on line 7
How To Fix Warning: Invalid argument supplied for foreach() in […] Error?
Method #1: Turn The Object Into An Array
Running the code:
<?php
$names = array(‘T’, ‘E’, ‘C’, ‘H’, ‘N’, ‘O’, ‘L’, ‘O’, ‘G’, ‘Y’);
foreach($mystring as $ms){
echo ' '.$ms;
}
?>
Output:
T E C H N O L O G Y
You can see that the foreach()
method in the program above is given the input $names, which is no longer “not” an array.
Accordingly, the built-in array()
function creates an array from $mystring. In order to print the items of the array with space between them, the loop iterates through the array as specified in the code.
Method #2: Compel Conversion of a String to Array
Running the code:
<?php
$names = 'TECHNOLOGY';
$names = is_array($names) ? $names : array();
foreach($names as $ms){
echo ' '.$ms;
}
?>
Output
TECHNOLOGY
This piece of code shows how to turn a string into an array. You can behold that the $mystring variable is accepted as an argument by the is array()
function.
The second line’s conditional expression determines if the variable is an array or not. If it returns false, the array()
function is used to turn the $mystring into an array.
The echo statement is then used to print out the array’s contents after iterating over them using the foreach()
technique.
Method #3: Program to Determine Whether an Object Is an Array
Running the code:
<?php
$names = array(‘T’, ‘E’, ‘C’, ‘H’, ‘N’, ‘O’, ‘L’, ‘O’, ‘G’, ‘Y’);
if(is_array($names) || is_object($names)){
foreach($names as $ms){
echo ' '.$ms;
}
}
?>
Output
T E C H N O L O G Y
In this case, the array contained in the variable $mystring is tested to see if it is an array or an object. As such, two methods, an array()
and an object, are employed inside the if statement ().
The is array()
function is then of great use to determine whether a variable is an array or not. Checking if a variable is an object or not is done using the is object()
method. The if clause determines if $mystring is an array or an object.
The foreach()
function iterates over the array elements and uses the echo statement to show them if either of the conditions is True since there is an OR between the expressions.
The outcome demonstrates that there is a pause between the letters.
Conclusion
Above are neatly what you would consider when solving the Warning: Invalid argument supplied for foreach() in […] error. Hopefully, this post can be helpful to you somehow!
Leave a comment