如何从多维数组中找到最大的数组


How to find the largest array from a multi dimensional array

可能重复:
从多维数组中的元素中获取最大值
在php 中查找特定多维数组值的max()

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

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

AFAIK数组的大小是根据它所包含的元素数量来计算的。

所以这可能有助于

$largeArraySize = 0;
foreach($arraylist as $array) {
   if(count($array) > $largeArraySize) {
     $largeArray = $array;
   }
}
// Hence $largeArray has the largest
print_r($largeArray);

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

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

如果没有其他信息,例如数组是否已经根据票数排序,您剩下的唯一选项是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...
}

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

$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'];
    }
}

将代码更改为:

$large=array('vote' => -1);
foreach($final2 as $f1){
    if ($f1['vote'] > $large['vote']) {
         $large=$f1;
    }
}
$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值。