Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there a way to count the values of a multidimensional array()?

$families = array
(
"Test"=>array
(
  "test1",
  "test2",
  "test3"
)
); 

So for instance, I'd want to count the 3 values within the array "Test"... ('test1', 'test2', 'test3')?

share|improve this question
count( array[0] ) did that work? – Drewdin Aug 24 '11 at 13:57
Going to guess no, because it's an associative array. That was my original answer and embarrassingly wrong! – Matt Aug 24 '11 at 14:00

5 Answers

up vote 3 down vote accepted
$families = array
(
"Test"=>array
(
  "test1",
  "test2",
  "test3"
)
); 

echo count($families["Test"]);
share|improve this answer
Thanks, Dogbert that's perfect. :) – Adam Aug 24 '11 at 14:00
2  
If it is perfect then you should upvote it and accept. – Layke Aug 24 '11 at 14:02
Give me chance :) – Adam Aug 24 '11 at 14:04
i was sooo close... – Drewdin Aug 24 '11 at 14:06

The count method must get you there. Depending on what your actual problem is you maybe need to write some (recursive) loop to sum all items.

share|improve this answer

A static array:

echo 'Test has ' . count($families['test']) . ' family members';

If you don't know how long your array is going to be:

foreach($families as $familyName => $familyMembers)
{
    echo $familyName . ' has got ' . count($familyMembers) . ' family members.';
}
share|improve this answer
function countArrayValues($ar, $count_arrays = false) {
    $cnt = 0;
    foreach ($ar as $key => $val) {
        if (is_array($ar[$key])) {
            if ($count_arrays)
                $cnt++;
            $cnt += countArrayValues($ar);
        }
        else
            $cnt++;
    }
    return $cnt;
}

this is custom function written by me, just pass an array and you will get full count of values. This method wont count elements which are arrays if you pass second parameter as false, or you don't pass anything. Pass tru if you want to count them also.

$count = countArrayValues($your_array);
share|improve this answer

I think I've just come up with a rather different way of counting the elements of an (unlimited) MD array.

<?php

$array = array("ab", "cd", array("ef", "gh", array("ij")), "kl");

$i = 0;
array_walk_recursive($array, function() { global $i; return ++$i; });
echo $i;

?>

Perhaps not the most economical way of doing the count, but it seems to work! You could, inside the anonymous function, only add the element to the counted total if it had a non empty value, for example, if you wanted to extend the functionality. An example of something similar could be seen here:

<?php

$array = array("ab", "cd", array("ef", "gh", array("ij")), "kl");

$i = 0;
array_walk_recursive($array, function($value, $key) { global $i; if ($value == 'gh') ++$i; });
echo $i;

?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.