【问题标题】:How to get top 3 Positions (Values) from array in PHP Including Duplicate如何从 PHP 中的数组中获取前 3 个位置(值),包括重复
【发布时间】:2020-02-25 06:12:53
【问题描述】:

我有一个按降序排列的数组,例如值100 , 98, 96, 90 ...。 我正在使用foreach() 循环遍历数组并使用if condition 限制3,例如`

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

获得最高的3 职位。但问题是,如果存在两个相同的值,例如100 , 98, 98, 96, 90 . .,那么限制应该从3 增加到4,以便在位置2 上存在两个值98, 98 和位置3 包含值@ 987654333@ 而不是 2nd 98 请记住,我需要一个位置上的两个重复数字,例如两个具有相同分数的学生站在一个位置上。 提前谢谢

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    检查解决方案:

    $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++;         
         }
      }
    }
    

    【讨论】:

    • 重复号码不必连续
    • @mahesh 有问题,您告诉您按 desc 顺序对数组进行排序。那么值是连续的。
    • 在循环之前,您只需要在循环语句之前按降序对数组进行排序。
    • @AnkurMishra 数组在循环之前按降序排序。
    • 它工作正常。打开链接并点击执行代码检查输出:sandbox.onlinephpfunctions.com/code/…
    【解决方案2】:

    看看这个

       $len=3;
        $selectedArray=[]
        $i = 0;
    
        while($i <= $len) {
     array_push($selectedArray, $array[$i]);
    
        if(in_array($array[$i], $array))
        {$len++;
        }
    
            $i++;
        }
    

    【讨论】:

      【解决方案3】:

      这个也很好用

      <?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(); }
      } 
      
      
      ?> 
      

      结果:php sandbox

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

      【讨论】:

        【解决方案4】:

        我不确定这是您要查找的内容,但您可以使用以下代码作为示例

            $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)); 
            }
        

        结果

        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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-06
          • 2018-02-06
          • 1970-01-01
          • 2018-07-07
          • 1970-01-01
          • 2018-03-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多