2

I have an array $MyArray which has some elements which are also array (lets call them subarrays). I want to know how many elements the subarray with the most elements has. The problem is, that I don't know if the index exists:

 max(
     @count($MyArray[$i*7]), 
     @count($MyArray[$i*7+1]), 
     @count($MyArray[$i*7+2]),         
     @count($MyArray[$i*7+3]),
     @count($MyArray[$i*7+4]),
     @count($MyArray[$i*7+5]),
     @count($MyArray[$i*7+6])
 );

Struckture of $MyArray:

Array(
  12 => array ( 
        0 => array ( 0 => 0, 1 => 1, ), 
        1 => array ( 0 => 13, 1 => 1, ), 
        2 => array ( 0 => 15, 1 => 1, ), 
        3 => array ( 0 => 20, 1 => 1, ), 
        4 => array ( 0 => 69, 1 => 1, ) 
  ),
  5 => array ( 
        0 => array ( 0 => 55, 1 => 1, ), 
        1 => array ( 0 => 32, 1 => 1, ), 
        2 => array ( 0 => 12, 1 => 1, ), 
        3 => array ( 0 => 21, 1 => 5, ) 
  ),
  ....
)

Can this be done better (faster)?

edit: I know foreach and I don't want to loop over every element in this array. I just want an interval of it. @ is used, because I don't know if $MyArray[$i*7 + x] is Null or an array.

$i is a element of [0, 1, 2, 3, 4] (sometimes also 5)

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • 1
    so, basically you have one array with many subarrays, and the goal is to get the subarray with the highest number of element and count it's total elements? – KJYe.Name Jan 19 '11 at 17:41
  • @kjy112, goal is to just count that subarray's elements, not even get that subarray. – shamittomar Jan 19 '11 at 17:45
  • 1
    PHP error suppression, `@`, when overused is the devil – brian_d Jan 19 '11 at 17:45
  • 1
    I don't get how most the answers use `foreach` and completely ignore the `$i*7+y` bit, especially since `$MyArray` structure is unknown... – netcoder Jan 19 '11 at 17:57
  • @netcoder: I upvoted your answer for doing that, but I don't feel the question is fully clear yet. – Jonah Jan 19 '11 at 18:13

5 Answers5

4
$biggest = 0;
foreach ($MyArray as $value) {
    if ($biggest < count($value)) {
        $biggest = count($value);
    }
}

I see, you want the size of the biggest array in the array, correct?

Jonah
  • 9,991
  • 5
  • 45
  • 79
  • I'd upvote you, but you are unnecessarily calling `count` twice – brian_d Jan 19 '11 at 17:48
  • 1
    @brain_d: I'm pretty sure that's not a concern. `count()` just gets the size, it doesn't actually _count_ them. And storing it into an array just creates a variable, which uses up processing. Plus, caching is not necessary to convey the concept. Thanks for the observation though. – Jonah Jan 19 '11 at 17:51
  • 2
    +1 I was not aware it was O(1). http://stackoverflow.com/questions/4566314/php-what-is-the-complexity-i-e-o1-on-of-the-function-count – brian_d Jan 19 '11 at 18:25
3

Simple and old school approach:

<?php

$max = -1;
foreach($MyArray as $subarray)
{
    $items = count($subarray);
    if($items > $max)
         $max = $items;
}

echo $max;
?>

This works best since you only want to know how many elements the subarray with the most elements has.

shamittomar
  • 46,210
  • 12
  • 74
  • 78
3
$max = 0;
foreach ($MyArray as $value) {
    $max = max($max,count($value));
}
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
2

Try this:

$arr = array();
for ($j=0;$j<=6;$j++) {
   if (isset($MyArray[$i*7+$j])) $arr[] = count($MyArray[$i*7+$j]);
}
$result = max($arr);

I don't know exactly what $i refers to though...

netcoder
  • 66,435
  • 19
  • 125
  • 142
  • Hi netcoder, thanks for your script. It seems like it is the fastest (see http://martin-thoma.blogspot.com/2011/01/benchmarking-php.html). I thought using more functions (`isset` + `count`) would take more time than simply surpressing the warnings with `@` and don't using `isset`. – Martin Thoma Jan 21 '11 at 07:10
1
// get the interesting part of the array
$chunk = array_intersect_key($input_array, array_flip(range($i*7, $i*7+6))); 
// max(count)
$max = $chunk ? max(array_map('count', $chunk)) : 0;
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • Well, this wasn't exactly right, but almost. range gives values, but I need keys: $chunk = array_intersect_key($MyArray, array($i*7=>1, $i*7+1=>2, $i*7+2=>3, $i*7+3=>4, $i*7+4=>5, $i*7+5=>6)); if(count($chunk) > 0){$max = max(array_map('count', $chunk));} else {$max = 0;} – Martin Thoma Jan 21 '11 at 05:54
  • ah you are right; then `$chunk = array_intersect_key($input_array, array_flip(range($i*7, $i*7+6))); $max = $chunk ? max(array_map('count', $chunk)) : 0;` – Arnaud Le Blanc Jan 21 '11 at 09:16