adv Posted January 16, 2009 Share Posted January 16, 2009 hello i`m trying to loop over and over the arrays and try to find some words the thing is it doesnt work .. i wanna find even a piece of it but its not working and cant understand why..... $array=array('first','second','third','3423a','3423'); $c=count($array); for($i=0;$i<$c;$i++){ $result=strpos($array[$i],'3423'); if($result!==false){ echo 'found it'; }else{ echo 'NOT found it'; } } Quote Link to comment Share on other sites More sharing options...
Mark Baker Posted January 16, 2009 Share Posted January 16, 2009 if($result!==false){ die; }else{ die; } You're always executing die after testing the first entry in the array, whether success or failure, so of course you never get to check the second Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 you're not finding it because first comes before second, and "sec" doesn't exist in "first" and either way if it matches or doesn't match, you die() the script.. take the die() out of the else statement and you should be okay.. <?php $array=array('first','second','third','3423a','3423'); foreach($array as $arrays){ $result=strpos($arrays,'sec'); if($result!==false){ echo 'found it<br />'; die; } else { echo 'NOT found it<br />'; } } ?> Quote Link to comment Share on other sites More sharing options...
adv Posted January 16, 2009 Author Share Posted January 16, 2009 ahh i modify it and figure about die; russel i tried ur way... but it finds both ways NOT found it found it $array=array('first','second','third','3423a','76676'); foreach($array as $arrays){ $result=strpos($arrays,'76'); if($result!==false){ echo 'found it<br />'; die; } else { echo 'NOT found it<br />'; } } i`ve use it like this edit : ive tried like that and it shows i understand why but i dont know how to limit to only the valid one NOT found it NOT found it NOT found it NOT found it found it Quote Link to comment Share on other sites More sharing options...
RussellReal Posted January 16, 2009 Share Posted January 16, 2009 I figured you wanted it to show when it didn't find it.. remove the else statement all together $array=array('first','second','third','3423a','76676'); foreach($array as $arrays){ $result=strpos($arrays,'76'); if($result!==false){ echo 'found it<br />'; die; } } Quote Link to comment Share on other sites More sharing options...
premiso Posted January 16, 2009 Share Posted January 16, 2009 ahh i modify it and figure about die; russel i tried ur way... but it finds both ways NOT found it found it $array=array('first','second','third','3423a','76676'); foreach($array as $arrays){ $result=strpos($arrays,'76'); if($result!==false){ echo 'found it<br />'; die; } else { echo 'NOT found it<br />'; } } i`ve use it like this edit : ive tried like that and it shows i understand why but i dont know how to limit to only the valid one NOT found it NOT found it NOT found it NOT found it found it Instead of die; you could just use break; $array=array('first','second','third','3423a','76676'); foreach($array as $arrays){ $result=strpos($arrays,'76'); if($result!==false){ echo 'found it<br />'; break; } } echo ' Break allows for more processing here.'; Quote Link to comment Share on other sites More sharing options...
adv Posted January 16, 2009 Author Share Posted January 16, 2009 thanks alot i see know is there just one thing that i want to know if there`s possible is there a way to echo just one value if it doesnt find any results... something like this if($result===false){ echo 'not found'; // loop through all the arrays and if no results in ALL of them just show that echo } Quote Link to comment Share on other sites More sharing options...
Maq Posted January 17, 2009 Share Posted January 17, 2009 Create a boolean. $array=array('first','second','third','3423a','76676'); $foundOne = FALSE; foreach($array as $arrays){ $result=strpos($arrays,'76'); if($result!==false){ echo 'found it '; $foundOne = TRUE; break; } } if($foundOne == FALSE) echo "You didn't find anything"; echo ' Break allows for more processing here.'; Quote Link to comment Share on other sites More sharing options...
corbin Posted January 17, 2009 Share Posted January 17, 2009 Edit: Beaten to it, but the Post button is glaring at me . I would probably wrap it in a function: function InValue($array, $valpiece) { foreach($array as $val) { if(strpos($array, $valpiece) !== false) return true; } return false; } if(InValue($array, '76')) echo 'Found!'; else echo 'Not found .'; If you don't want it in a function: $found = false; foreach($array as $val) { if(strpos($array, $valpiece) !== false) { $found = true; break; } } if($found) echo 'Found!'; else echo 'Not found .'; Quote Link to comment Share on other sites More sharing options...
adv Posted January 17, 2009 Author Share Posted January 17, 2009 thanks alot friends its works good corbin u misspelled function InValue($array, $valpiece) { foreach($array as $val) { // $val instead of array if(strpos($val, $valpiece) !== false) return true; } return false; } if(InValue($array, '76')) echo 'Found!'; else echo 'Not found .'; anyway thanks alot edit : ahh just instead of array Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.