Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the 1st 5 largest values from an array

Tags:

arrays

php

i am trying to get the 1st 5 largest values from a numeric array...i have tried using the rsort() function to list the array values from highest to lowest but cant get a way to pick the 1st 5 from the result.

like image 553
Elric Wamugu Avatar asked Sep 11 '26 08:09

Elric Wamugu


2 Answers

Using array_slice:

$a = array ( 1, 3, 4, 2, 4, 5, 10, 7, 6, 8, 0 );
rsort($a);
$largest = array_slice($a, 0, 5);
like image 66
knittl Avatar answered Sep 13 '26 20:09

knittl


Check this out

$array_b4_change=array("knittl", "limón", "naranja", "plátano", "manzana" , "vikas" ,"wazzzy");
rsort($array_b4_change);

Use

array_slice($array_b4_change, 0, 5);
like image 26
Wazy Avatar answered Sep 13 '26 22:09

Wazy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!