How to find the largest array from a multidimensional array

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

I am trying to find the largest array from a multidimensional 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 with the highest voices. Highest means the array has the largest voice

I used the following code but it doesn't work.

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

                        foreach($final2 as $f2){

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

                        }

                    }

      

+3


source to share


6 answers


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

Maybe this will help



$largeArraySize = 0;

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

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

      

If no large array arrives, this code will accept the first occurrence as the largest.

+1


source


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



0


source


Without additional information like the array is already sorted by number of votes, the only option you have is linear search 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...
}

      

0


source


This should give you an idea of ​​getting the most votes in the internal 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'];
    }
}

      

0


source


Change your code to:

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

      

0


source


$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 contain all arrays with the most votes being the value of $ max_votes.

0


source







All Articles