By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,726 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Loop logic problem

100+
P: 384
Only 'test' is ever output, I know its something to do with the if loop but can't put my finger on the root cause, basically the string is input and exploded and each value is checked against the $permittedPlatforms and if it is allowed then is is returned.

The input is: test,test2,blue
Expand|Select|Wrap|Line Numbers
  1. private function processPlatform($platformsIn) {
  2.         $permittedPlatforms = array('test','test2','blue');
  3.         $platforms = '';
  4.         $permittedSize = sizeof($permittedPlatforms);
  5.         $size = 0;
  6.         $count = 0;
  7.         $permittedCount = 0;
  8.  
  9.         $platformsIn = explode(",",$platformsIn);
  10.         $size = sizeof($platformsIn);
  11.  
  12.         while($count < $size) {
  13.             while($permittedCount < $permittedSize) {
  14.                 if($permittedPlatforms[$permittedCount] == $platformsIn[$count]) {
  15.                     if($platforms != '')
  16.                         $platforms .= ', '.$platformsIn[$count];
  17.                     else
  18.                         $platforms .= $platformsIn[$count];
  19.                 }
  20.                 $permittedCount++;
  21.             }
  22.             $count++;
  23.         }
  24.  
  25.         return $platforms;
  26.     }
May 4 '10 #1
Share this Question
Share on Google+
2 Replies


code green
Expert 100+
P: 1,726
With a loop witin a loop you need to ensure the counts are reset to zero.

But take a look at array_intersect() which does all that work for you
May 4 '10 #2

100+
P: 384
Spotted where I needed to reset one of the counts, thanks for your help.
May 4 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.