Array Search Problem

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

    Array Search Problem

    I'm trying to search an array for a string and if the string exists in the array it wont go into the if statement, I'm using the below code, It just wont work for me, any ideas?
    Code:
    if((!in_array($string,$Array)) && ($string != '')) {
    		print $string.'<br/>';
    }
  • ShenPixel
    New Member
    • Oct 2009
    • 5

    #2
    This is what I do, would that be practical?

    Code:
    if ( in_array ( $string, $array) ){
    echo 'The ' .$string. ' is in the array';
    }
    else
    {
    echo 'The ' .$string. ' is not in the array';
    }

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      Originally posted by ziycon
      Code:
      if(([u]![/u]in_array($string,$Array)) && ($string != '')) {
      		print $string.'<br/>';
      }
      translated into words:
      if string is not in array and string is not empty then print string.

      ...exactly what has happened.

      although I'd test for the string first (the second expression is not executed if the first expression evaluates to FALSE)
      Code:
      if ($string && in_array(...))

      Comment

      • ziycon
        Contributor
        • Sep 2008
        • 384

        #4
        Basicly if the string doesn't exist in the array and the string isn't empty then it will print out but whats happening is even if the string is in the array it still prints out the string, which it shouldn't do!

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          can you give an example to reproduce, because that sounds unlikely?

          Comment

          • ziycon
            Contributor
            • Sep 2008
            • 384

            #6
            $outputArrayTem p[$outputCountTem p] has just been assigned a string.

            The strings would be links like 'http://www.google.ie' etc.

            Code:
            if((!in_array($outputArrayTemp[$outputCountTemp],$outputArrayTemp)) && ($outputArrayTemp[$outputCountTemp] != '')) {
                print $outputArrayTemp[$outputCountTemp].'<br/>';
                $outputCountTemp++;
            }

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              Another bit of information, the links are printing out fine, the function about is basicly to stop any link being printed out twice. So say that http://www.google.ie is in $outputArrayTem p and say the link http://www.google.ie is found four times then its printing like:






              When it shouldn't print out as it exists in $outputArrayTem p.

              Comment

              • ziycon
                Contributor
                • Sep 2008
                • 384

                #8
                Ok, heres what I have so far, the full function. Hopefully this makes it easier to understand what I'm trying to do. Any help is much appreciated.
                Code:
                $linkCount = sizeof($matches[1]);
                
                while($count < $linkCount) {
                	$string = $matches[1][$count];
                
                	if($string[0] == '/') {
                		$outputArrayTemp[$outputCountTemp] = $domain.$string;
                		$outputCountTemp++;
                	}
                	else if(substr($string,0,4) == 'http') {
                		$outputArrayTemp[$outputCountTemp] = $string;
                		$outputCountTemp++;
                	}
                
                	if((!in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)) && ($outputArrayTemp[$outputCountTemp-1] != '')) {
                		$outputArray[$outputCount] = $outputArrayTemp[$outputCountTemp-1];
                		$outputCount++;
                	}
                	$count++;
                }

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  did you define the start values of $outputCountTem p and $count anywhere?

                  Comment

                  • ziycon
                    Contributor
                    • Sep 2008
                    • 384

                    #10
                    There both defined as zero, think I have it working withe the below code:
                    Code:
                    $linkCount = sizeof($matches[1]);
                    
                    while($count < $linkCount) {
                    	$string = $matches[1][$count];
                    
                    	if(($string[0] == '/') || (substr($string,0,4) == 'http')) {
                    		if($string[0] == '/') {
                    			$outputArrayTemp[$outputCountTemp] = $domain.$string;
                    			$outputCountTemp++;
                    		}
                    		else if(substr($string,0,4) == 'http') {
                    			$outputArrayTemp[$outputCountTemp] = $string;
                    			$outputCountTemp++;
                    		}
                    
                    		if(!in_array($outputArrayTemp[$outputCountTemp-1],$outputArray)) {
                    			$outputArray[$outputCount] = $outputArrayTemp[$outputCountTemp-1];
                    			$outputCount++;
                    		}
                    	}
                    	$count++;
                    }

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      Code:
                      !in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)
                      this will always evaluate to false

                      Comment

                      • ziycon
                        Contributor
                        • Sep 2008
                        • 384

                        #12
                        Originally posted by Dormilich
                        Code:
                        !in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)
                        this will always evaluate to false
                        I know, I fixed it, was meant to be:
                        Code:
                        !in_array($outputArrayTemp[$outputCountTemp-1],$outputArray)
                        That was the main problem with it not working as far as I can tell.

                        Comment

                        Working...