How to find the Kth smallest number in an unsorted array with PHP

1 Answer

0 votes
function findKthSmallestNumber(array $arr, int $k): int {
    sort($arr); // Sorts the array in ascending order
    
    return $arr[$k - 1];
}

$arr = [42, 90, 21, 30, 37, 81, 45];
$k = 3;

$result = findKthSmallestNumber($arr, $k);
echo $result . PHP_EOL; 



/*
run:

37

*/

 



answered 1 day ago by avibootz
...