【问题标题】:How to find the largest array from a multi dimensional array [duplicate]如何从多维数组中找到最大的数组[重复]
【发布时间】:2012-03-17 12:01:12
【问题描述】:

可能重复:
Get the maximum value from an element in a multidimensional array?
find max() of specific multidimensional array value in php

我试图从多维数组中找出最大的数组。

Array
(
    [0] => Array
        (
            [comment] => ayya
            [commented_on] => 17/03/12
            [ckey] => 210029c5d80d8259d1599c9a
            [username] => pappa
            [up] => 2
            [down] => 0
            [vote] => 2
        )

    [1] => Array
        (
            [comment] => sdfsd
            [commented_on] => 17/03/12
            [ckey] => 08f6a34f96bdeef2903ddaf4
            [username] => jesse
            [up] => 2
            [down] => 0
            [vote] => 2
        )

    [2] => Array
        (
            [comment] => 159
            [commented_on] => 17/03/12
            [ckey] => 4da385124793336339268782
            [username] => jesse
            [up] => 2
            [down] => 0
            [vote] => 2
        )

    [3] => Array
        (
            [comment] => s
            [commented_on] => 17/03/12
            [ckey] => 299c77c52ee087e468e23e82
            [username] => jesse
            [up] => 2
            [down] => 0
            [vote] => 2
        )

    [4] => Array
        (
            [comment] => jh
            [commented_on] => 17/03/12
            [ckey] => 523c18820d8b8db827a240ad
            [username] => jesse
            [up] => 2
            [down] => 0
            [vote] => 2
        )

    [5] => Array
        (
            [comment] => jh
            [commented_on] => 17/03/12
            [ckey] => 9f824c11b0ecafcc38c09f4c
            [username] => jesse
            [up] => 1
            [down] => 1
            [vote] => 0
        )

    [6] => Array
        (
            [comment] => jh
            [commented_on] => 17/03/12
            [ckey] => c97e7ad4d205220c4b8b0332
            [username] => jesse
            [up] => 1
            [down] => 0
            [vote] => 1
        )

)

我想获得票数最高的数组。 最高表示投票数最高的数组

我使用了以下代码,但它不起作用。

$large=array();
                    foreach($final2 as $f1){

                        foreach($final2 as $f2){

                            if($f1['vote']>$f2['vote'])
                                $large=$f1;

                        }

                    }

【问题讨论】:

  • $large=array(); foreach($final2 as $f1){ foreach($final2 as $f2){ if($f1['vote']>$f2['vote']) $large=$f1; } } op($大);
  • 请编辑您的问题并添加您的代码。在 cmets 中是不可读的。
  • @sunilkumar 你似乎走在了正确的轨道上,我假设第二个foreach 是复制和粘贴错误,因为它不需要。
  • 刚刚为它写了一个oneliner $maxs = $multi[array_keys(array_map(function( $row ){ return count($row['vote']); }, $multi ), max(array_map(function( $row ){ return count($row['vote']); }, $multi )))[0]];

标签: php arrays


【解决方案1】:

AFAIK 数组的大小是从它拥有的元素数中计算出来的。

也许这会有所帮助

$largeArraySize = 0;

foreach($arraylist as $array) {
   if(count($array) > $largeArraySize) {
     $largeArray = $array;
   }
}

// Hence $largeArray has the largest
print_r($largeArray);

除非出现一个大数组,否则此代码将第一次出现作为最大的。

【讨论】:

  • 每次发现新的更大数组时,您需要将 $largeArraySize 更改为等于 count($array);
  • 或 1 班轮:$max = max( array_map(function( $row ){ return count($row); }, $multi ) )
【解决方案2】:

由于数组只嵌套了一层并且所有子数组都具有相同的结构,因此只需循环遍历外部数组并跟踪到目前为止看到的最大值。漂亮又简单。

【讨论】:

    【解决方案3】:

    如果没有其他信息,例如数组是否已按投票数排序,您剩下的唯一选择是O(n) 线性搜索。

    $max = 0;
    $max_index = 0;
    
    if( count($outer_array) > 0 )
    {
        // There are elements in the outer array
        for($i = 0; $i < count($outer_array); $i++ )
        {
            if( isset($outer_array[$i]["vote"]) )
            {
                if( $outer_array[$i]["vote"] > $max )
                {
                    $max_index = $i;
                }
            }
            else
            {
                // Error condition, malformed array
                // Do something here, maybe throw exception?
            } 
        }
    }
    else
    {
        // Error condition - outer array is empty, could also throw exception here...
        $max = -1; // Assume votes cannot be negative
        $max_index = -1;
    }
    
    if( $max_index != -1 )
    {
        // Success
        // Do something...
    }
    

    【讨论】:

      【解决方案4】:

      这应该让您了解在内部数组中获得最高票数:

      $array = array(
          array('votes' => 2),
          array('votes' => 3),
          array('votes' => 0),
          array('votes' => 1)
      );
      
      $votes = 0;
      $key = 0;
      for($i = 0; $i < count($array); $i++){
          if($array[$i]['votes'] > $votes){
          $key = $i;
          $votes = $array[$i]['votes'];
          }
      }
      

      【讨论】:

        【解决方案5】:

        将您的代码更改为:

        $large=array('vote' => -1);
        foreach($final2 as $f1){
            if ($f1['vote'] > $large['vote']) {
                 $large=$f1;
            }
        }
        

        【讨论】:

          【解决方案6】:
          $array = array(.....)
          
          $max_votes = 0;
          foreach ($array as $data) {
              $vote = $data['vote'];
              if ($vote > $max_vote) {
                  $max_vote = $vote;
              }     
          }
          
          $max = array();
          foreach ($array as $key => $data) {
             if ($data['vote'] == $max_vote) {
                 $max[] = $key;
             }
          }
          

          $max 现在将保存所有票数最高的数组,即 $max_votes 的值。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-06-24
            • 2017-06-19
            • 1970-01-01
            • 2018-04-26
            • 1970-01-01
            • 2021-11-10
            • 1970-01-01
            • 2012-04-17
            相关资源
            最近更新 更多