March 17, 2012
How to find the largest array from a multi dimensional array
Question by sunil kumar
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;
}
}
Answer by Starx
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.