3v4l.org

run code in 300+ PHP versions simultaneously
<?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; }
Output for 7.1.25 - 7.1.28, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.13
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" } }

preferences:
81.02 ms | 410 KiB | 5 Q