Forum Moderators: coopster

Message Too Old, No Replies

getting an error when looping multi array

can anyone spot the mistake?

         

proper_bo

11:38 am on Nov 16, 2005 (gmt 0)

10+ Year Member



I have come up with code that works fine on my local machine, but spits an error when it goes live on the site. I can only think it is due to the php version.
But, just incase its the code, here is the error line:

foreach($this->details[$code] as $value){
. foreach($value as $option){
. . $spin = $option['option_type'];
. . array_push($totals, $spin);
. } //fe
}// fe

is there anything wrong with that?

the error I am fed is:
Warning: Invalid argument supplied for foreach() in /home/www/site/folder/file.class on line 263

coopster

12:39 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Which of those lines is line 263? Have you tried dumping the variables in question on that line to see what type they are (string, array, etc.) and what values they contain?

dreamcatcher

12:49 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The foreach error usually occurs if the variable being processed is not array.

dc

proper_bo

12:55 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



Hi guys,

sorry, line 263 is the first line of the inner of the two foreach loops.

I think you are correct in that it is erroring because the input is not an array. Some of the values going to it are and some are not. Is there an easy way to stop the errors?
maybe something along the lines of checking if the input is an array?

foreach($this->details[$code] as $value){
. if(Is_array($value){
. . foreach($value as $option){
. . . $spin = $option['option_type'];
. . . array_push($totals, $spin);
. . } //fe
. } //fi
}// fe

would that work?

dreamcatcher

4:27 pm on Nov 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, because the foreach is still doing the same thing. You wantt to check a) the data is an array or b) the array is not empty BEFORE you run the foreach loop.

dc

proper_bo

4:35 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



I implemented the change above and it is working fine.

the problem line is the one after the new 'if' call.

The problem was that there were 21 items that were not arrays and then one item that was.

the if(is_array$blah) line sorted that by doing nothing for all the 21 that were not arrays but allowing the 22nd to go to the next stage.

Should I be doing it another way?

taylanpince

5:46 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



no you got it right, all you needed to do was to check and make sure that the variable you fed to the foreach statement was an array. if you want to use the variables that are not arrays, you can throw in an else statement as well.

charlier

5:54 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



Another way is to cast your variable to an array then it will have at least one array elelemt.

$MyArray = (array) $MyMightBeAnArrayMightNot;

proper_bo

10:44 pm on Nov 16, 2005 (gmt 0)

10+ Year Member



thanks for all your help.
both the else suggestion and changing everything to an array are both clever solutions.