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

时间:2012-03-17 12:01:12

标签: php arrays

  

可能重复:
  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;

                        }

                    }

6 个答案:

答案 0 :(得分:1)

AFAIK数组大小从其拥有的元素数量开始计算。

所以可能会有所帮助

$largeArraySize = 0;

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

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

除非有大数组,否则此代码将首次出现为最大值。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

如果没有其他信息,例如数组是否已按投票数排序,您唯一的选择是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...
}

答案 3 :(得分:0)

这应该可以让你在内部数组中获得最多票数:

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

答案 4 :(得分:0)

将您的代码更改为:

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

答案 5 :(得分:0)

$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的值。