[PHP] Array - Match
I have $sub = abc; and $subs[0] = cde; $subs[0] = iyu; $subs[0] = abc; .. .. .. $subs[50] = xyx; How to find whether $sub matches with any one of $subs[$i] I have used a for loop but it is returning true when $subs[$i] = xabc. /Chandu --- Powered by MihiraMail, Fast secure reliable Brought to you by Mihira Net Pvt Ltd. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array - Match
? $sub = abcd; $subs[] = cde; $subs[] = iyu; $subs[] = abc; $subs[] = xyx; $match=false; foreach($subs as $value) { if($sub==$value){$match=true; break;} } if($match) echo 'found!'; else echo 'not found!'; //or better use //for PHP 4 if(in_array($sub,$subs))echo 'found!'; else echo 'not found!'; //also possible //for PHP 4 = 4.0.5 4.2.0 if(is_null(($b=array_search($sub,$subs echo 'not found!'; else echo 'found! In $sub['.$b.']'; //or for PHP 4 = 4.2.0 if(($b=array_search($sub,$subs))) echo 'found! In $sub['.$b.']'; else echo 'not found!'; ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Array - Match
Hello Chandu, You can use in_array(needle, haystack) for this. For example, if (in_array(abc,$subs)) {item found, do stuff...} HTH! Jed On the threshold of genius, N. Pari Purna Chand wrote: I have $sub = abc; and $subs[0] = cde; $subs[0] = iyu; $subs[0] = abc; .. .. .. $subs[50] = xyx; How to find whether $sub matches with any one of $subs[$i] I have used a for loop but it is returning true when $subs[$i] = xabc. /Chandu --- Powered by MihiraMail, Fast secure reliable Brought to you by Mihira Net Pvt Ltd. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php