Jump to content

selecting words out of an array?


spires

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/96179-selecting-words-out-of-an-array/
Share on other sites

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?

<?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'])

?>

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);
?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.