2

I am creating a word search but I want to rank them base on the highest existence of search keyword. How can I solve this problem?

I am trying to make a search if array 1 key exists inside array 2 long string and then order the array by total occurrence of array 1 in array 2.

Blow is my code

$str = "Hello World January Jude";
$arr1 = ["Hello World January Jude Lol Love","Hello Lol Loop","Love Life Jude","Crude Flash Hello"];
$str = explode(" ", $str);
echo sort_base($arr1, $str);
function sort_base($arr, $str){
$count = "";
foreach ($arr as $valuer){
    foreach ($str as $value){
    //$list[] = strpos($valuer, $value, 0);
    $count .= strpos($valuer, $value, 0)."<hr/>"; 
}

}
$arr = trim($count," ");
 echo $arr;
}

Example input:

$array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; 
$str = "Hello World Cup"; 

Desire output:

Array in order:

  1. Hello World Cup
  2. Final Hello World
  3. Hello Cup
  4. Hello
  5. World
  6. Say Hello
dWinder
  • 11,597
  • 3
  • 24
  • 39
  • Hello, can you explain in more detail how the output of that function is different from the output you're trying to get? Examples of expected and actual output would be helpful. – Don't Panic Apr 16 '19 at 21:49
  • ok i will drop here For Example am searching for "**Hello World Cup**" which is $str as variable and i have arrays with example $array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; so i want to sort my array $array base on the one with the most existing search keywords from the string i split earlier which is "**Hello World Cup**" using $str = explode(" ",$array). – High Breed Technology Apr 16 '19 at 21:58
  • so for example $array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; $str = "Hello World Cup"; so if i call function like this $arrays = sort_most_exists_asc($str, $array); it should Reorder my array so my array should be sorted to Hello World Cup Final Hello World Hello Cup Hello World Say Hello – High Breed Technology Apr 16 '19 at 22:10
  • Possible duplicate of [Javascript sort an array by matching to string](https://stackoverflow.com/questions/8451409/javascript-sort-an-array-by-matching-to-string) – Marc Apr 16 '19 at 22:23
  • so for example $array = ["Say Hello","Hello World"," Hello World Cup Final","Hello Cup","Hello","World"]; $str = "Hello World Cup"; so if i call function like this $arrays = sort_most_exists_asc($str, $array); it should Reorder my array so my array should be sorted to 1. Hello World Cup 2. Final Hello World 3. Hello Cup 4. Hello 5. World 6. Say Hello Re Arranged base on total indexed array and return back as array for use. – High Breed Technology Apr 16 '19 at 22:24

2 Answers2

1

You can use array-intersect and count to achieve number value of the similar words. Now you can use usort for sort by that.

Consider the following:

function sort_most_exists_asc($arr, $str) {
    usort($arr, function ($a, $b) use ($str) {
        $aa = count(array_intersect(explode(" ", $str), explode(" ", $a)));
        $bb = count(array_intersect(explode(" ", $str), explode(" ", $b)));
        return $bb - $aa;
    });
    return $arr;
}

$str = "Hello World January Jude";
$arr = ["Hello World January Jude Lol Love","Hello Lol Loop","Love Life Jude","Crude Flash Hello"];
$arr = sort_most_exists_asc($arr, $str);

Live example: 3v4l

Notice this will work only for whole words. For words similarity use Levenshtein distance - and compare by that in the usort

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • thanks!, but i don't know how to use this when it is not a function nor variable? – High Breed Technology Apr 16 '19 at 22:28
  • @FlashMediaTechnology What do you mean? what is not a function nor variable? I converted it to function like - better now? – dWinder Apr 16 '19 at 22:34
  • above code was corrected to function sort_most_exists_asc($array, $str) { usort($array, function ($a, $b) use ($str) { $aa = count(array_intersect(explode(" ", $str), explode(" ", $a))); $bb = count(array_intersect(explode(" ", $str), explode(" ", $b))); return $bb - $aa; }); return $array; } now working with no error thanks a lot @dWinder – High Breed Technology Apr 16 '19 at 22:41
  • @FlashMediaTechnology - if this helped you may mark it as accepted (the grey "v" at the left of the answer). However, Don't_panic answer seems more elegant so recommend you try his code also. and edit the var name to `$arr` – dWinder Apr 16 '19 at 22:43
  • Hello this really solve my problem but can not work with Multi dimensional array Eg: $arr = array(array("title" => "Hello World", "body" => "Hi All this is my content"), array("title" => "Hello World Boy", "body" => "Hi All this is my content Girl"),array("title" => "Hello Kids", "body" => "Hi All this is my content Kid")); $str = "Hello World January Jude"; $arr = sort_most_exists_asc($arr, $str); But Does not seems to work at all. – High Breed Technology Apr 17 '19 at 09:49
  • Hi @dWinder you are highly needed please. – High Breed Technology Apr 17 '19 at 10:04
  • You may want to flatten the multi-dimension array first with something like `array_reduce($array, 'array_merge', array())`. However, this is different question as you change the question constrain - so it it better to open new question explaining what you have and what you want to achieve – dWinder Apr 17 '19 at 10:39
  • I don't think i need further questions have done something at lease, look at what have done so far [**3v4**](https://3v4l.org/TYVLu) worked well here but fails to work with fetched data from database and am using fetch(PDO::FETCH_ASSOC) with it @dWinder see please. – High Breed Technology Apr 17 '19 at 10:48
  • The code example you have look fine. I need to see the code of how you retrieve the data from DB – dWinder Apr 17 '19 at 11:05
  • i use $q = $_GET['q']; $query_i = "SELECT * FROM `pages` WHERE MATCH(title, description, url) AGAINST ('".$q."' IN BOOLEAN MODE) "; $Query = $DB->flash->query($query_i); if($Query->rowCount() > 0){ while($row = $Query->fetch(PDO::FETCH_ASSOC)){ $rowz = sort_most_exists_asc($row, $q); var_dump($rowz); } } – High Breed Technology Apr 17 '19 at 11:18
  • and the output was fatal Error: Warning: Illegal string offset 'title' in /opt/lampp/htdocs/sep/sep.class.php on line 100 Notice: Uninitialized string offset: 0 in /opt/lampp/htdocs/sep/sep.class.php on line 101 – High Breed Technology Apr 17 '19 at 11:19
  • Notice: Uninitialized string offset: 0 in /opt/lampp/htdocs/sep/sep.class.php on line 101 array(6) { [0]=> string(2) "36" [1]=> string(30) "Shopping Cart - Tech Plus Host" [2]=> string(0) "" [3]=> string(48) "https://tech.com/billing/cart.php?gid=10" [4]=> string(10) "1555364327" [5]=> string(0) "" } – High Breed Technology Apr 17 '19 at 11:19
  • This happens because `$row` is single row and as far as I understand you want to sort all the result. You should do `$rows = $page->fetchAll(PDO::FETCH_ASSOC);` and then `$arr2 = sort_most_exists_asc($rows, $str);` – dWinder Apr 17 '19 at 11:24
  • thanks a lot, your tutorial helped me, i don't know how i will thank you again, if i could get any where we can connect and chat so i can give you credit on my project page after am done with my project?? either FB,IG or TWITTER? @dWinder. – High Breed Technology Apr 17 '19 at 12:03
  • You are most welcome. I glad to help! this the whole idea of this site. No recognition is needed - just remember to help other whenever you can - "pay it forward" – dWinder Apr 17 '19 at 12:14
  • good day @dWinder this result is arranging by following case sensitivity at first please i don't know how to do it in a way it will follow case insensitivity and follow only the rule **MOST EXISTING STRING IN ARRAY** without case sensitivity Eg: $str = pray for me and $arr = Pray for me now, pray till now, pray to me the given code chooses pray till now and pray to me before Pray for me now because of case sensitivity please help me on this thanks. – High Breed Technology Apr 17 '19 at 17:53
  • You can use `strtolower` as: `$str = strtolower("Hello World January Jude");` and in the function: `explode(" ", strtolower($a))` (and same for `$b`). You should really start new question for those follow up or try define your constrain better - this is allow the question to be better format for future users – dWinder Apr 17 '19 at 19:16
  • i am sorry i return result base on my final findings from sort_most_exists_asc($arr, $str) if i make $str lowercase and i am returning back lowercase it's a very bad idea but maybe i will look for another way round thanks a lot. – High Breed Technology Apr 17 '19 at 23:00
  • https://stackoverflow.com/questions/55738873/is-there-any-php-function-to-solve-out-array-value-matches-in-another-array-valu as suggested by you sir @dWinder I created new Question about that need help on it please. – High Breed Technology Apr 18 '19 at 04:03
0

You can create a set of all the matching words between each phrase and the target list of words.

foreach ($arr1 as $phrase) {
    $matches[] = array_intersect(str_word_count($phrase, 1), $str);
}

That array of matches can be used with array_multisort to reorder the original array.

array_multisort($matches, SORT_DESC, $arr1);
Don't Panic
  • 41,125
  • 10
  • 61
  • 80