Tuesday 5 February 2013

Avoid Invalid argument supplied for foreach()



Looping array we different ways such as while,foreach and for loop.
We use one of the above technique base on the situation.
Take an example we have and array of key not as 0,1,2…..
in such situation we cannot use for loop so we should use
foreach.We cannot say the input supplied is always an array
some time we may get empty value for that and we get this
error
“Invalid argument supplied for foreach()..”
To avoid this we can use the
functions
key(Mixed array); // Which returns the key of current array element
current(Mixed array); // Which returns the value of current array element
next(Mixed array); /// Which moves to pointer to next array element
Instead of using like this
foreach($array as $tmp){
echo $tmp;
}
we can use
for($i=0;$i<count($array);$i++){
key($array);
current($array);
next($array);
}
both do the same process

No comments:

Post a Comment

If any doubt?then please comment in my post

How to reduce angular CLI build time

 Index: ----- I am using angular cli - 7 and I am going to tell how to reduce the build time as per my knowledge. Problem: -------- Now the ...