spires Posted March 14, 2008 Share Posted March 14, 2008 Hi Guys. Is it posible to select a unique word in an array, and display all the words the has that words in. Example: Array list: pink blackberry blackberry case blackberry phone blackberry 8800 pink headset case To be selected list: pink case Results: pink blackberry blackberry 8800 pink blackberry case headset case All of the pinks appear, then all of the caes appear. So far I have this (both arrays) but i need to know how to select them. $list = $_POST['list']; $list_array = explode("\n", $list); $cat = $_POST['cat']; $cat_array = explode("\n", $cat); foreach ($list_array as $lists) { foreach ($cat_array as $cats) { $lists = trim($lists); $cats = trim($cats); } } } Thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/ Share on other sites More sharing options...
OkBoy Posted March 14, 2008 Share Posted March 14, 2008 I'm not sure what you mean "select them". The easiest thing would be in your first foreach to do something like: if (strpos($lists,"pink" > 0) echo $lists And then do something similar for the second loop. I am sorta new to this too, but the strpos would be a way to check for the substring within the string, yeah? Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492429 Share on other sites More sharing options...
spires Posted March 14, 2008 Author Share Posted March 14, 2008 It's a good idea, but I can't seem to get it to work. The problem is that out of 100 words, 10 will have the word pink in. So I need to display that batch together. I'm sure that there is an easy way. I just have to find it. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492437 Share on other sites More sharing options...
Psycho Posted March 14, 2008 Share Posted March 14, 2008 <?php function match_categories($list, $cat) { //Create the arrays $list_array = explode("\n", $list); $cat_array = explode("\n", $cat); //clean the data foreach ($list_array as $key => $value) $list_array[$key] = trim($value); foreach ($cat_array as $key => $value) $cat_array = trim($value); $return_vals = array(); //Search each category for each list item foreach ($cat_array as $category) { foreach ($list_array as $list_item) { if (strpos($category,$list_item)) { $return_vals[$list_item] = $category; } } } } $found_categories = match_categories($_POST['list'], $_POST['cat']) ?> Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492469 Share on other sites More sharing options...
spires Posted March 14, 2008 Author Share Posted March 14, 2008 Great I'll try it out. Cheers Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492478 Share on other sites More sharing options...
Barand Posted March 14, 2008 Share Posted March 14, 2008 You need to check for !== false with strpos(). If the item starts with searchstring it returns 0 which will also be treated as false. if (strpos($category,$list_item) !== false) { Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492532 Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 or try <?php $a = 'pink blackberry blackberry case blackberry phone blackberry 8800 pink headset case'; $c = 'pink case'; $c = str_replace(' ','',$c); $c = str_replace("\n",'|',$c); echo $out = preg_replace("/(^.*(\b($c)\b).*$)|(^.*\n?\r?)/m",'$1',$a); ?> Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492583 Share on other sites More sharing options...
laffin Posted March 14, 2008 Share Posted March 14, 2008 Came up with this one. using preg_match. also added a simple scoring system, which than sorts the matches by score. <?php $list=array('pink blackberry','blackberry case','pink blackberry case','blackberry phone','blackberry 8800 pink','headset case'); $search=array('pink','case'); function rsearch($list,$keys) { $scores=array(); foreach($keys as $term) { $term=preg_quote($term,'/'); foreach($list as $key=>$slist) { $score=(preg_match("/^$term\$/i",$slist)*1000) + // complete match highest score (preg_match("/\b$term\b/i",$slist)*100) + // Within search list, Med Score preg_match("/$term/i",$slist); // compounded search term, low score if($score) $scores[$key]+=$score; } } $keys=array_keys($scores); $items=count($keys); for($i=0;$i<($items-1);$i++) { for($j=1;$j<$items;$j++) { if($scores[$keys[$i]]<$scores[$keys[$j]]) $keys[$j] ^= $keys[$i] ^= $keys[$j] ^= $keys[i]; } } foreach($keys as $key) $matches[]=$list[$key]; return $matches; } header('Content-type: text/plain'); print_r(rsearch($list,$search)); ?> U can prolly add a lot more to the scoring system. maybe adding weights to the search terms position. Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492584 Share on other sites More sharing options...
Psycho Posted March 14, 2008 Share Posted March 14, 2008 Just found an error in the code I posted above: This line $return_vals[$list_item] = $category; Should be $return_vals[$list_item][] = $category; Quote Link to comment https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/#findComment-492595 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.