Loop logic problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    Loop logic problem

    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 $permittedPlatf orms and if it is allowed then is is returned.

    The input is: test,test2,blue
    Code:
    private function processPlatform($platformsIn) {
    		$permittedPlatforms = array('test','test2','blue');
    		$platforms = '';
    		$permittedSize = sizeof($permittedPlatforms);
    		$size = 0;
    		$count = 0;
    		$permittedCount = 0;
    
    		$platformsIn = explode(",",$platformsIn);
    		$size = sizeof($platformsIn);
    		
    		while($count < $size) {
    			while($permittedCount < $permittedSize) {
    				if($permittedPlatforms[$permittedCount] == $platformsIn[$count]) {
    					if($platforms != '')
    						$platforms .= ', '.$platformsIn[$count];
    					else
    						$platforms .= $platformsIn[$count];
    				}
    				$permittedCount++;
    			}
    			$count++;
    		}
    
    		return $platforms;
    	}
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    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

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      Spotted where I needed to reset one of the counts, thanks for your help.

      Comment

      Working...