Array Search Problem | Familiar Sight | | Join Date: Sep 2008
Posts: 252
| |
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? - if((!in_array($string,$Array)) && ($string != '')) {
-
print $string.'<br/>';
-
}
| | Newbie | | Join Date: Oct 2009
Posts: 5
| | | re: Array Search Problem
This is what I do, would that be practical? -
-
if ( in_array ( $string, $array) ){
-
echo 'The ' .$string. ' is in the array';
-
}
-
else
-
{
-
echo 'The ' .$string. ' is not in the array';
-
}
-
-
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Array Search Problem Quote:
Originally Posted by ziycon - if((!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) - if ($string && in_array(...))
| | Familiar Sight | | Join Date: Sep 2008
Posts: 252
| | | 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!
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Array Search Problem
can you give an example to reproduce, because that sounds unlikely?
| | Familiar Sight | | Join Date: Sep 2008
Posts: 252
| | | re: Array Search Problem
$outputArrayTemp[$outputCountTemp] has just been assigned a string.
The strings would be links like 'http://www.google.ie' etc. - if((!in_array($outputArrayTemp[$outputCountTemp],$outputArrayTemp)) && ($outputArrayTemp[$outputCountTemp] != '')) {
-
print $outputArrayTemp[$outputCountTemp].'<br/>';
-
$outputCountTemp++;
-
}
| | Familiar Sight | | Join Date: Sep 2008
Posts: 252
| | | 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. - $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++;
-
}
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Array Search Problem
did you define the start values of $outputCountTemp and $count anywhere?
| | Familiar Sight | | Join Date: Sep 2008
Posts: 252
| | | re: Array Search Problem
There both defined as zero, think I have it working withe the below 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++;
-
}
|  | Moderator | | Join Date: Aug 2008 Location: Leipzig, Germany
Posts: 3,629
| | | re: Array Search Problem - !in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)
this will always evaluate to false
| | Familiar Sight | | Join Date: Sep 2008
Posts: 252
| | | re: Array Search Problem Quote:
Originally Posted by Dormilich - !in_array($outputArrayTemp[$outputCountTemp-1],$outputArrayTemp)
this will always evaluate to false I know, I fixed it, was meant to be: - !in_array($outputArrayTemp[$outputCountTemp-1],$outputArray)
That was the main problem with it not working as far as I can tell.
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|