I am creating a word search but I want to rank them base on the highest existence of search keyword. 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.
Example input:
$arr = array(array("id" => 1,"title" => "Hello World", "body" => "Hi Jude All this is my content"),
array("id" => 2,"title" => "Hello World Boy", "body" => "Hi All this is my content Girl"),
array("id" => 3,"title" => "Hello Kids", "body" => "Hi All this is my content Kid"),
array("id" => 4,"title" => "Hello World Jude", "body" => "Hi All this is my content Jude"),
array("id" => 5,"title" => "Hello World Jude January", "body" => "Hi All this is my content Jan"),
array("id" => 6,"title" => "Hello World January June Lord", "body" => "Hi All this is my content Jan Jude Lord"));
$str = "Hello world January jude";
Desire output:
Array in order:
Hello World Jude January
Hello World Jude
Hello World January June Lord
Hello World
Hello World Boy
Hello Kids
I wrote a question earlier base on solving problem for Is there any PHP function to solve out array value matches in another array value string? afterward I got solution on that but my main problem now is that it is judging my filter base on case sensitivity if my search keyword is hello world
and I have Hello world
and hi world
because the world on hi world
is lowercase as the one at my search keywords it picks the one with the lowercase first before considering the one with the most matches I have tried several things but I could not get it.
Note: I want the Output to be returned the way it is not to return in lowercase format.
This is what I tried using the example input from above:
$arr2 = sort_most_exists_asc($arr, $str);
var_dump($arr2);
function sort_most_exists_asc($array, $str) {
usort($array, function ($a, $b) use ($str) {
$aa = count(array_intersect(explode(" ", $str), explode(" ", $a['title'])));
$bb = count(array_intersect(explode(" ", $str), explode(" ", $b['title'])));
return $bb - $aa;
});
return $array;
}
Worked well if its formatted exactly like that but I don't want it to follow case sensitivity during the array_intersect.