How to get the top 5 integers from an array of 20 in PHP

1 Answer

0 votes
// An array of 20 integers
$numbers = [5, 69, 62, 51, 43, 88, 31, 90, 6, 53, 100, 40, 26, 3, 64, 99, 73, 86, 30, 2];

// Sort the array in descending order
rsort($numbers);

// Get the top 5 integers
$maxFive = array_slice($numbers, 0, 5);

print_r($maxFive);



/*
run:

Array
(
    [0] => 100
    [1] => 99
    [2] => 90
    [3] => 88
    [4] => 86
)

*/

 



answered Jul 8, 2025 by avibootz
...