darkminos Posted July 22, 2012 Share Posted July 22, 2012 Hi, I have a multidimensional (2 levels) array and would like to filter the results where: $myArray[$i]['title'] = '$keywords' Basically I want the filter to only display results which contain any of the $keywords in the title Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/ Share on other sites More sharing options...
requinix Posted July 22, 2012 Share Posted July 22, 2012 Considering the generic terms "filter" and "results", I'll just say you should probably use a loop. Or two. Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363419 Share on other sites More sharing options...
Barand Posted July 22, 2012 Share Posted July 22, 2012 Are you pulling the data for this array from a database? Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363421 Share on other sites More sharing options...
darkminos Posted July 22, 2012 Author Share Posted July 22, 2012 Nope, results are pulled from various websites, serialized into multiple files, on search - unserialized into one array and sorted. And now I want to display only the results of an array which ['title'] contains 'oil OR painting' Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363422 Share on other sites More sharing options...
xyph Posted July 22, 2012 Share Posted July 22, 2012 <?php $keywords = array('foo','bar'); foreach( $myArray as $subArray ) { if( in_array($subArray['title'], $keywords) ) { var_dump($subArray); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363504 Share on other sites More sharing options...
darkminos Posted July 22, 2012 Author Share Posted July 22, 2012 Thanks for the reply, unfortunately in_array doesn't work with multidimensional arrays... Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363542 Share on other sites More sharing options...
darkminos Posted July 22, 2012 Author Share Posted July 22, 2012 Should it not be foreach ($myArray as $subArray) { if(in_array($keywords, $subArray['title']) ) { var_dump($subArray); } } Anyhow ... no results displayed. This is what the array looks like array(925) { [0]=> array(9) { ["pic"]=> string(53) "http://ecx.images-amazon.com/images/I/21HPIhRQNFL.jpg" ["link"]=> string(225) "http://www." ["from"]=> string(6) "A" ["title"]=> string(99) "20 x 18W, PAR38, AC100-240V, E27 Edison ES, LED Lamp, Warm White, 75W-100W Equivalent, Non-Dimmable, Bulb" ["cost"]=> string(6) "7.80" ["description1"]=> string(37) "Direct replacement for 75W-100W bulbs" ["description2"]=> string(23) "Energy saving up to 80%" ["description3"]=> string(18) "High quality light" ["category"]=> string(3) "LED" } And now if the search keyword is "led bulb" it should look for "led" and "bulb" in the ['title'], if one of the keywords is identified the result should be positive. Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363547 Share on other sites More sharing options...
darkminos Posted July 22, 2012 Author Share Posted July 22, 2012 Ok... I see now, in_array is looking for an exact match... while what I have to do is probably divide $keyword and $myArray into strings and compare this way... am I on the right track? Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363592 Share on other sites More sharing options...
xyph Posted July 23, 2012 Share Posted July 23, 2012 Thanks for being more specific on what you wanted. In my first example, I'm not calling in_array on a multi-dimensional array, but it still won't do what you want, as it's checking for an exact match, rather than it simply containing the keyword. <?php $keywords = array('foo','bar'); $myArray = array( array('title'=>'blah blah blah','description'=>'key 1'), array('title'=>'foo blah','description'=>'key 2'), array('title'=>'blah blah bar','description'=>'key 3'), array('title'=>'blah blah blah','description'=>'key 4'), array('title'=>'bar blah foo','description'=>'key 5') ); // Loop through each sub-array of myArray foreach( $myArray as $subArray ) { // Loop through each keyword foreach($keywords as $keyword) { // Check if 'title' in the sub-array contains the current keyword if( stristr($subArray['title'],$keyword) !== FALSE ) { // If so, report it echo "Match found!"; // Dump the matching sub-array var_dump($subArray); // Stop looking for more keywords for this sub-array. break; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363612 Share on other sites More sharing options...
darkminos Posted July 23, 2012 Author Share Posted July 23, 2012 Works perfectly, thank you very much! Quote Link to comment https://forums.phpfreaks.com/topic/266070-filter-array-by-keyword/#findComment-1363619 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.