3

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;

                        }

                    }
Community
  • 1
  • 1
sunil kumar
  • 35
  • 3
  • 8
  • $large=array(); foreach($final2 as $f1){ foreach($final2 as $f2){ if($f1['vote']>$f2['vote']) $large=$f1; } } op($large); – sunil kumar Mar 17 '12 at 12:05
  • Please edit your question and add your code. It's unreadable in comments. – Felix Kling Mar 17 '12 at 12:06
  • @sunilkumar you seem to be on the right track, I'm assuming the second `foreach` is a copy & paste error since it's not needed. – John Carter Mar 17 '12 at 12:08
  • just wrote a oneliner for it `$maxs = $multi[array_keys(array_map(function( $row ){ return count($row['vote']); }, $multi ), max(array_map(function( $row ){ return count($row['vote']); }, $multi )))[0]];` – DarkMukke Aug 07 '15 at 14:55

6 Answers6

1

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.

Starx
  • 77,474
  • 47
  • 185
  • 261
0

Since the array is only nested one level deep and all the sub-arrays have the same structure, just loop through the outer array and track the maximum value seen so far. Nice and simple.

John Carter
  • 53,924
  • 26
  • 111
  • 144
0

Without additional information such as whether the array is already sorted on number of votes, the only option you have left is a O(n) linear search.

$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...
}
ose
  • 4,065
  • 2
  • 24
  • 40
0

This should give you an idea to get the highest number of votes in the inner arrays:

$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'];
    }
}
alxbrd
  • 1,675
  • 1
  • 15
  • 16
0

Change your code to:

$large=array('vote' => -1);
foreach($final2 as $f1){
    if ($f1['vote'] > $large['vote']) {
         $large=$f1;
    }
}
Toto
  • 89,455
  • 62
  • 89
  • 125
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 will now hold all of the arrays with the highest number of votes being the value of $max_votes.

Tidy Designs
  • 153
  • 8