【发布时间】: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]];