joecooper Posted March 24, 2013 Share Posted March 24, 2013 (edited) Hi, I've just spent the last several hours speeding though coding a sudden idea I had. Now I'm feeling a bit brain dead and cannot get my head around the next part. I have a string array, results[0] which is full of file names. I need to create a quick way to search though those names, and list in order of relativity / order of closest match. The search term would normally be more than 1 word, so the ones that match all the words, then going on to the ones that match fewer words etc. Although there are around 100 - 200 strings in the array, I would only need to display the top 5 results. Any help would be appriated. Thanks Joe! Edited March 24, 2013 by joecooper Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/ Share on other sites More sharing options...
PaulRyan Posted March 24, 2013 Share Posted March 24, 2013 (edited) Try something like this: <?PHP $searchTerm = 'search'; $items = array('test search.jpg', 'test.jpg', 'search.png', 'love.png', 'testicle.png', 'looool.php'); foreach($items AS $file) { similar_text($searchTerm, $file, $percent); $final[$file] = $percent; } arsort($final); $final = array_slice($final, 0, 5); echo '<pre>'; print_r($final); ?> *Edit - Added array_slice to get first 5 elements. Edited March 24, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/#findComment-1420804 Share on other sites More sharing options...
joecooper Posted March 24, 2013 Author Share Posted March 24, 2013 Hi, Thanks for that! seems to be what I were looking for. Just that it outputs like this: Array( [http://www.SOMESITE.net/Eminem/Encore/07 Puke.mp3] => 20.3389830508 [http://www.SOMESITE.net/Eminem/Encore/06 Mosh.mp3] => 20.3389830508 [http://www.SOMESITE.net/Eminem/Relapse/06 Hello.mp3] => 19.6721311475 [http://www.SOMESITE.net/Eminem/Relapse/04 Insane.mp3] => 19.3548387097 [http://www.SOMESITE.net/Eminem/Relapse/03 My Mom.mp3] => 19.3548387097) Possible for it to just output the URL? Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/#findComment-1420807 Share on other sites More sharing options...
PaulRyan Posted March 24, 2013 Share Posted March 24, 2013 Just put this after the array_slice() part. foreach($final AS $key => $value) { $topItems[] = $key; } print_r($topItems); $topItems[0] = Top Item $topItems[1] = Second Top Item etc etc Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/#findComment-1420808 Share on other sites More sharing options...
Jessica Posted March 25, 2013 Share Posted March 25, 2013 A. There's a function called array_keys B. just change the code to put the value in the value instead of the key. Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/#findComment-1420812 Share on other sites More sharing options...
PaulRyan Posted March 25, 2013 Share Posted March 25, 2013 If you put the value in the value, then you can't sort by the percentage comparison. There is was to do a callback function, which I can't remember how. array_keys, I have a feeling something like that existed, didn't get a good chance to look through the array functions. Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/#findComment-1420813 Share on other sites More sharing options...
Jessica Posted March 25, 2013 Share Posted March 25, 2013 Switch the keys and values and use ksort, or just use array_keys Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/#findComment-1420814 Share on other sites More sharing options...
PaulRyan Posted March 25, 2013 Share Posted March 25, 2013 array_keys is what I'd go for, will have to have a read up on the array functions I think. Quote Link to comment https://forums.phpfreaks.com/topic/276103-search-though-a-string-array/#findComment-1420816 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.