1

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

4 Answers 4

1

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++;         
     }
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

it is not necessary that duplicate number are consecutive
@mahesh in question you tell that you sort array in desc order. Then values are consecutive.
Before loop you just need to sort array in desc order before loop statement.
@AnkurMishra array is sorted before loop in descending order.
It is working fine. Open link and click on execute code to check output: sandbox.onlinephpfunctions.com/code/…
|
0

Have a look at this

   $len=3;
    $selectedArray=[]
    $i = 0;

    while($i <= $len) {
 array_push($selectedArray, $array[$i]);

    if(in_array($array[$i], $array))
    {$len++;
    }

        $i++;
    }

Comments

0

Also this, will work well

<?php

$array = array(100,98,98,98,96,96,90);
$arr_counter = 0;
$items = array();
foreach($array as $arr){


   $items[] = $arr; //capture all values in this cycle

   #get all values that are repeating then add it to a new array
   $nrr = array_unique(array_diff_assoc($items, array_unique($items)));

    #If the number is repeating again dont add to our counter $arr_counter
   if (!in_array($arr, $nrr)) {  $arr_counter = $arr_counter+1; }  

   echo $arr;  echo ' | position '; echo $arr_counter; echo '<br>';

   if  ($arr_counter == 3) { die(); }
} 


?> 

Result: php sandbox

100 | position 1
98 | position 2
98 | position 2
98 | position 2
96 | position 3

Comments

0

I'm not sure this is what are you looking for, but you can use below code as example

    $a = [100, 100, 98, 97, 97, 97, 96];
   
    foreach(array_slice(array_count_values($a), 0, 3, true) as $number => $repeat) {
        var_dump(array_fill(0, $repeat, $number)); 
    }

Result

array(2) {
  [0]=>
  int(100)
  [1]=>
  int(100)
}
array(1) {
  [0]=>
  int(98)
}
array(3) {
  [0]=>
  int(97)
  [1]=>
  int(97)
  [2]=>
  int(97)
}

Live Sandbox

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.