<?php
$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";
$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_uintersect(explode(" ", $str), explode(" ", $a['title']), 'strcasecmp'));
$bb = count(array_uintersect(explode(" ", $str), explode(" ", $b['title']), 'strcasecmp'));
return $bb - $aa;
});
return $array;
}
array(6) {
[0]=>
array(3) {
["id"]=>
int(5)
["title"]=>
string(24) "Hello World Jude January"
["body"]=>
string(29) "Hi All this is my content Jan"
}
[1]=>
array(3) {
["id"]=>
int(4)
["title"]=>
string(16) "Hello World Jude"
["body"]=>
string(30) "Hi All this is my content Jude"
}
[2]=>
array(3) {
["id"]=>
int(6)
["title"]=>
string(29) "Hello World January June Lord"
["body"]=>
string(39) "Hi All this is my content Jan Jude Lord"
}
[3]=>
array(3) {
["id"]=>
int(1)
["title"]=>
string(11) "Hello world"
["body"]=>
string(30) "Hi Jude All this is my content"
}
[4]=>
array(3) {
["id"]=>
int(2)
["title"]=>
string(15) "Hello world Boy"
["body"]=>
string(30) "Hi All this is my content Girl"
}
[5]=>
array(3) {
["id"]=>
int(3)
["title"]=>
string(10) "hello Kids"
["body"]=>
string(29) "Hi All this is my content Kid"
}
}