Connecting Tech Pros Worldwide Help | Site Map

Array Search Problem

Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#1: Oct 14 '09
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?
Expand|Select|Wrap|Line Numbers
  1. if((!in_array($string,$Array)) && ($string != '')) {
  2.         print $string.'<br/>';
  3. }
Newbie
 
Join Date: Oct 2009
Posts: 5
#2: Oct 14 '09

re: Array Search Problem


This is what I do, would that be practical?

Expand|Select|Wrap|Line Numbers
  1.  
  2. if ( in_array ( $string, $array) ){
  3. echo 'The ' .$string. ' is in the array';
  4. }
  5. else
  6. {
  7. echo 'The ' .$string. ' is not in the array';
  8. }
  9.  
  10.  
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#3: Oct 14 '09

re: Array Search Problem


Quote:

Originally Posted by ziycon View Post

Expand|Select|Wrap|Line Numbers
  1. if((!in_array($string,$Array)) && ($string != '')) {
  2.         print $string.'<br/>';
  3. }

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)
Expand|Select|Wrap|Line Numbers
  1. if ($string && in_array(...))
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#4: Oct 14 '09

re: Array Search Problem


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!
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#5: Oct 14 '09

re: Array Search Problem


can you give an example to reproduce, because that sounds unlikely?
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#6: Oct 14 '09

re: Array Search Problem


$outputArrayTemp[$outputCountTemp] has just been assigned a string.

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

Expand|Select|Wrap|Line Numbers
  1. if((!in_array($outputArrayTemp[$outputCountTemp],$outputArrayTemp)) && ($outputArrayTemp[$outputCountTemp] != '')) {
  2.     print $outputArrayTemp[$outputCountTemp].'<br/>';
  3.     $outputCountTemp++;
  4. }
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#7: Oct 15 '09

re: Array Search Problem


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 $outputArrayTemp and say the link http://www.google.ie is found four times then its printing like:

http://www.google.ie
http://www.google.ie
http://www.google.ie
http://www.google.ie

When it shouldn't print out as it exists in $outputArrayTemp.
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#8: Oct 15 '09

re: Array Search Problem


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.
Expand|Select|Wrap|Line Numbers
  1. $linkCount = sizeof($matches[1]);
  2.  
  3. while($count < $linkCount) {
  4.     $string = $matches[1][$count];
  5.  
  6.     if($string[0] == '/') {
  7.         $outputArrayTemp[$outputCountTemp] = $domain.$string;
  8.         $outputCountTemp++;
  9.     }
  10.     else if(substr($string,0,4) == 'http') {
  11.         $outputArrayTemp[$outputCountTemp] = $string;
  12.         $outputCountTemp++;
  13.     }
  14.  
  15.     if((!in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)) && ($outputArrayTemp[$outputCountTemp-1] != '')) {
  16.         $outputArray[$outputCount] = $outputArrayTemp[$outputCountTemp-1];
  17.         $outputCount++;
  18.     }
  19.     $count++;
  20. }
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#9: Oct 15 '09

re: Array Search Problem


did you define the start values of $outputCountTemp and $count anywhere?
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#10: Oct 15 '09

re: Array Search Problem


There both defined as zero, think I have it working withe the below code:
Expand|Select|Wrap|Line Numbers
  1. $linkCount = sizeof($matches[1]);
  2.  
  3. while($count < $linkCount) {
  4.     $string = $matches[1][$count];
  5.  
  6.     if(($string[0] == '/') || (substr($string,0,4) == 'http')) {
  7.         if($string[0] == '/') {
  8.             $outputArrayTemp[$outputCountTemp] = $domain.$string;
  9.             $outputCountTemp++;
  10.         }
  11.         else if(substr($string,0,4) == 'http') {
  12.             $outputArrayTemp[$outputCountTemp] = $string;
  13.             $outputCountTemp++;
  14.         }
  15.  
  16.         if(!in_array($outputArrayTemp[$outputCountTemp-1],$outputArray)) {
  17.             $outputArray[$outputCount] = $outputArrayTemp[$outputCountTemp-1];
  18.             $outputCount++;
  19.         }
  20.     }
  21.     $count++;
  22. }
Dormilich's Avatar
Moderator
 
Join Date: Aug 2008
Location: Leipzig, Germany
Posts: 3,629
#11: Oct 15 '09

re: Array Search Problem


Expand|Select|Wrap|Line Numbers
  1. !in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)
this will always evaluate to false
Familiar Sight
 
Join Date: Sep 2008
Posts: 252
#12: Oct 15 '09

re: Array Search Problem


Quote:

Originally Posted by Dormilich View Post

Expand|Select|Wrap|Line Numbers
  1. !in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)
this will always evaluate to false

I know, I fixed it, was meant to be:
Expand|Select|Wrap|Line Numbers
  1. !in_array($outputArrayTemp[$outputCountTemp-1],$outputArray)
That was the main problem with it not working as far as I can tell.
Reply