jarvis Posted May 1, 2014 Share Posted May 1, 2014 Hi All, Incredibly simple I'm sure! I have an if while loop with a counter. $i = 0; How can I rewrite this part : if($i == 1 || $i == 5 || $i == 9 || $i == 13 ){ I tried if($i % 4 === 0){ but that's not right Thanks Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 1, 2014 Share Posted May 1, 2014 If you're looking to split data in groups of 4, you could use array_chunk(): http://www.php.net/manual/en/function.array-chunk.php Quote Link to comment Share on other sites More sharing options...
jarvis Posted May 1, 2014 Author Share Posted May 1, 2014 Thanks cyberRobot, was hoping to just use something similar to the code I had if poss? Quote Link to comment Share on other sites More sharing options...
Solution cyberRobot Posted May 1, 2014 Solution Share Posted May 1, 2014 This seems to work: if($i % 4 == 1) { Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 1, 2014 Share Posted May 1, 2014 (edited) Did you think to test what the result of ($i % 4) would be for different values of $i? If you had it would have shown that you were on the right track, but made one flaw. for($i=0; $i<14; $i++) { echo "{$i} : mod 4 = " . ($i%4) . "<br>\n"; } Here are the results of that test (I've highlighted the ones that I think you should pay attention to: 0 : mod 4 = 01 : mod 4 = 12 : mod 4 = 23 : mod 4 = 34 : mod 4 = 05 : mod 4 = 16 : mod 4 = 27 : mod 4 = 38 : mod 4 = 09 : mod 4 = 110 : mod 4 = 211 : mod 4 = 312 : mod 4 = 0 13 : mod 4 = 1 EDIT: cyberRobot beat me to it (because he took the easy route ), but I wanted to hopefully teach you how to solve problems like this in the future. Edited May 1, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
jarvis Posted May 1, 2014 Author Share Posted May 1, 2014 Ah thank you! Quote Link to comment Share on other sites More sharing options...
Psycho Posted May 1, 2014 Share Posted May 1, 2014 You marked your own response as the best answer? Quote Link to comment Share on other sites More sharing options...
jarvis Posted May 2, 2014 Author Share Posted May 2, 2014 Am so sorry! I thought it was a 'mark as solved' overall, not a specific part of the thread. I've altered this now and would like to say thanks once again for the help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.