...

View Full Version : how to check if a string exists in an array?



Bobafart
01-24-2008, 09:37 PM
using PHP/mySQL combo

if I SELECT a row from my table which looks like this for instance:



SELECT * FROM myCats WHERE userid=1

userid col2 col3 col4 col5 col6 col7
1 hockey movies gaming tv music fashion

and store the results in an array (mysql_fetch_array)


how do I then compare a string to see if it exists in the array or not?

ie.:


if($myArray == 'hockey') {
echo 'hockey is one of your selections';
}else{
echo 'hockey isn't in your selections';
}

oesxyl
01-24-2008, 09:43 PM
using PHP/mySQL combo

if I SELECT a row from my table which looks like this for instance:



SELECT * FROM myCats WHERE userid=1

userid col2 col3 col4 col5 col6 col7
1 hockey movies gaming tv music fashion

and store the results in an array (mysql_fetch_array)


how do I then compare a string to see if it exists in the array or not?

ie.:


if($myArray == 'hockey') {
echo 'hockey is one of your selections';
}else{
echo 'hockey isn't in your selections';
}




if(in_array('hokey',$myarray)){
echo 'hockey is one of your selections';
}else{
echo 'hockey isn\'t in your selections';
}


I don't test this, if in_array don't work, could be because mysql_fetch_array is a hash array and I don't know how in_array work with this, you can use instead, array_search with same syntax.

best regards

Erindesign
01-25-2008, 07:26 PM
I looked at oesxyl code, and it seems ingeneous. I would have taken the info and put it in an array. Then run a simple index (for) loop from ur first array, to the last (colx) since x is the changing variable. If it matches with one of those, you are good.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum