EchoFool Posted June 5, 2011 Share Posted June 5, 2011 Hey, I'm trying to create a search system where by fields have a number in them but my php carries an array of numbers. So lets say a field holds the value 3. And my php array is 1,3,5,8,9 How can i simply query to check if the number in the field is found in the php array ? Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/ Share on other sites More sharing options...
Pikachu2000 Posted June 5, 2011 Share Posted June 5, 2011 1,3,5,8,9 is a string, not an array. You're going to need to be more descriptive of what it is you're actually wanting to do. Is there a database involved? Flat file? Or . . . ? Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/#findComment-1225641 Share on other sites More sharing options...
xyph Posted June 5, 2011 Share Posted June 5, 2011 You want to use in_array() http://php.net/in_array More reading http://php.net/manual/en/ref.array.php Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/#findComment-1225642 Share on other sites More sharing options...
EchoFool Posted June 5, 2011 Author Share Posted June 5, 2011 okay well i store numbers in database like 1,2,3,4,5 in a single field. then i explode the string $var = explode(',',$string); so now i got the array as $var then i need to find all rows with field "test" containing a number that is found in array $var. in_array cannot be done in a query from what im aware of? Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/#findComment-1225644 Share on other sites More sharing options...
xyph Posted June 5, 2011 Share Posted June 5, 2011 This is a DATABASE issue <?php $result = '1,2,3,4,5'; $query = 'SELECT `column` FROM `table` WHERE `field` = \'test\' AND `number` IN ('.$result.')'; echo $query; ?> Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/#findComment-1225662 Share on other sites More sharing options...
EchoFool Posted June 5, 2011 Author Share Posted June 5, 2011 but wouldn't that fail? if number = 2 2 != 1,2,3,4,5 ? As this is an int being checked in a string ? Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/#findComment-1225676 Share on other sites More sharing options...
xyph Posted June 5, 2011 Share Posted June 5, 2011 When you echo the query, you get this SELECT `column` FROM `table` WHERE `field` = 'test' AND `number` IN (1,2,3,4,5) Read more here http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#function_in Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/#findComment-1225678 Share on other sites More sharing options...
EchoFool Posted June 5, 2011 Author Share Posted June 5, 2011 oh okay - thanks Quote Link to comment https://forums.phpfreaks.com/topic/238511-search-for-results-in-a-php-array/#findComment-1225679 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.