How to get top 3 Positions (Values) from array in PHP Including Duplicate

Mani :

I have a sorted array in descending order such as with values 100 , 98, 96, 90 .... and I am using foreach() loop to iterate over the array and use if condition with limit 3 such as `

foreach($array as $arr){
if(loop_counter<3)
{ 
 echo 'something'; 
}}

to get top 3 positions. but the problem is that if there exist two same values such as 100 , 98, 98, 96, 90 . . then limits should increase from 3 to 4 so that on position 2 there exist two values 98, 98 and position 3 contain value 90 instead of 2nd 98 remember that I need both duplicate number on one position such as two students with same marks stands against one position. thanks in advance

Ankur Mishra :

Check the solution for this:

$array = [100, 98, 96, 96, 95];
$count = 0;
$length = count($array);
foreach($array as $key => $arr){
  if($count<3)
  { 
     echo $arr . ", ";
     if(($length != $key + 1) && $array[$key] != $array[$key + 1]) 
     {
        $count++;         
     }
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174187&siteId=1