如何找到一个多维数组的数组最大 [英] How to find the largest array from a multi dimensional array

查看:147
本文介绍了如何找到一个多维数组的数组最大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/2189479/get-the-maximum-value-from-an-element-in-a-multidimensional-array\">Get从元素多维数组中的最大值?结果
  <一href=\"http://stackoverflow.com/questions/5426972/find-max-of-specific-multidimensional-array-value-in-php\">find在PHP 特定多维数组值MAX()


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

 阵列

    [0] =&GT;排列
        (
            [评论] =&GT; ayya
            [commented_on] =&GT; 17/03/12
            [ckey] =&GT; 210029c5d80d8259d1599c9​​a
            [用户名] =&GT; PAPPA
            [置顶] =&GT; 2
            [下] =&GT; 0
            [投票] =&GT; 2
        )    [1] =&GT;排列
        (
            [评论] =&GT; sdfsd
            [commented_on] =&GT; 17/03/12
            [ckey] =&GT; 08f6a34f96bdeef2903ddaf4
            [用户名] =&GT;杰西
            [置顶] =&GT; 2
            [下] =&GT; 0
            [投票] =&GT; 2
        )    [2] =&GT;排列
        (
            [评论] =&GT; 159
            [commented_on] =&GT; 17/03/12
            [ckey] =&GT; 4da385124793336339268782
            [用户名] =&GT;杰西
            [置顶] =&GT; 2
            [下] =&GT; 0
            [投票] =&GT; 2
        )    [3] =&GT;排列
        (
            [评论] =&GT;小号
            [commented_on] =&GT; 17/03/12
            [ckey] =&GT; 299c77c52ee087e468e23e82
            [用户名] =&GT;杰西
            [置顶] =&GT; 2
            [下] =&GT; 0
            [投票] =&GT; 2
        )    [4] =&GT;排列
        (
            [评论] =&GT; JH
            [commented_on] =&GT; 17/03/12
            [ckey] =&GT; 523c18820d8b8db827a240ad
            [用户名] =&GT;杰西
            [置顶] =&GT; 2
            [下] =&GT; 0
            [投票] =&GT; 2
        )    [5] =&GT;排列
        (
            [评论] =&GT; JH
            [commented_on] =&GT; 17/03/12
            [ckey] =&GT; 9f824c11b0ecafcc38c09f4c
            [用户名] =&GT;杰西
            [置顶] =&GT; 1
            [下] =&GT; 1
            [投票] =&GT; 0
        )    [6] =&GT;排列
        (
            [评论] =&GT; JH
            [commented_on] =&GT; 17/03/12
            [ckey] =&GT; c97e7ad4d205220c4b8b0332
            [用户名] =&GT;杰西
            [置顶] =&GT; 1
            [下] =&GT; 0
            [投票] =&GT; 1
        ))

我想获得最高的有票阵列。
最高的是指具有最高的投票阵列

我用下面的code,但它不工作。

  $ =大阵列();
                    的foreach($ final2为$ F1){                        的foreach($ final2为$ F2){                            如果($ F1 ['投票']&GT; $ F2 ['投票'])
                                $大= $ F1;                        }                    }


解决方案

据我所知数组大小是由元素的数量有计数。

所以,可能是这将有助于

  $ largeArraySize = 0;的foreach($ ArrayList中为$数组){
   如果(计数($数组)&GT; $ largeArraySize){
     $ largeArray = $阵列;
   }
}//因此$ largeArray拥有最大
的print_r($ largeArray);

除非大阵来此code将第一次出现是最大的。

Possible Duplicate:
Get the maximum value from an element in a multidimensional array?
find max() of specific multidimensional array value in php

Iam trying to find out the largest array from multi dimensional array.

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
        )

)

I would like to get the array having highest votes. Highest means the array having highest vote

I have used the following code, but it is not working.

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

                        foreach($final2 as $f2){

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

                        }

                    }

解决方案

AFAIK array size is counted from the number of elements it has.

So may be this will help

$largeArraySize = 0;

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

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

Unless a large array come this code will take the first occurrence as the largest.

这篇关于如何找到一个多维数组的数组最大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆