0

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')?

asked Aug 24, 2011 at 13:55
2
  • count( array[0] ) did that work? Commented Aug 24, 2011 at 13:57
  • Going to guess no, because it's an associative array. That was my original answer and embarrassingly wrong! Commented Aug 24, 2011 at 14:00

5 Answers 5

4
$families = array
(
"Test"=>array
(
 "test1",
 "test2",
 "test3"
)
); 
echo count($families["Test"]);
answered Aug 24, 2011 at 13:57
Sign up to request clarification or add additional context in comments.

Comments

1

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;
?>
answered Aug 24, 2011 at 14:10

Comments

0

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.

answered Aug 24, 2011 at 14:00

Comments

0

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.';
}
answered Aug 24, 2011 at 14:01

Comments

0
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);
answered Aug 24, 2011 at 14:02

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.