8

I have an array like as

$arr[0] = 'summary';
$arr[1]['contact'][] = 'address1';
$arr[1]['contact'][] = 'address2';
$arr[1]['contact'][] = 'country';
$arr[1]['contact'][] = 'city';
$arr[1]['contact'][] = 'pincode';
$arr[1]['contact'][] = 'phone_no';
$arr[2]['work'][] = 'address1';
$arr[2]['work'][] = 'address2';
$arr[2]['work'][] = 'country';
$arr[2]['work'][] = 'city';
$arr[2]['work'][] = 'pincode';
$arr[2]['work'][] = 'phone_no';

Using count($arr) it returns 3 but I also need to count inner array values so it will return 13

What I've tried so far is

function getCount($arr,$count = 0) {

    foreach ($arr as $value) {

        if (is_array($value)) {
            echo $count;
            getCount($value,$count);
        }
        $count = $count+1;
    }
    return $count;
}

echo getCount($arr);

But its not working as expected

3
  • 7
    If you wish to count the values in the inner arrays as well(depth doesn't matter) you can pass in a second parameter to count as count($your_array, COUNT_RECURSIVE). Potentially if you want to count only the inner values of the array you can subtract the normal count from the recursive count. Commented Aug 17, 2015 at 11:43
  • 2
    @Andrew this will return 17 Commented Aug 17, 2015 at 11:51
  • 1
    Check this [multidimensional array count][1] [1]: stackoverflow.com/questions/9062770/… Commented Aug 17, 2015 at 11:53

4 Answers 4

7

You can use array_walk_recursive for this. This may help -

$tot = 0;
array_walk_recursive($arr, function($x) use(&$tot) {
    $tot++;
});

But It is a recursive function so you need to be careful about that.

In that getCount() method you are not storing the count of the array anywhere. So for every call $count is incremented by 1 only.

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

But where I'm wrong within my code in foreach loop because I might have nested array too this one already done. Anyway thanks
To fix your original function, I think you would either need to do $count = getCount($value,$count); or pass $count by reference.
4

Simply try this

function getCount($arr, $count = 0) {
    foreach ($arr as $value) {
        if (is_array($value)) {
            $count = getCount($value, $count);
        } else {
            $count = $count + 1;
        }
    }
    return $count;
}

echo getCount($arr);

What you were doing over here is that you were not storing the value within any variable and that is making an issues with your code as you were on the perfect way

Comments

4

If you need to count only the first two levels, you can do a simple foreach, no need to use a recursive function!:

$count = 0;
foreach( $bigArray as $smallArray ){
    if( is_array( $smallArray ) )
          $count += count( $smallArray );
    else
          $count ++;
}

Comments

0

Perhaps I am naive in my approach, but I simply use the sizeof() function. The function's documentation shows that it can take a second argument 1 to tell the function to recursively count multidimensional arrays.

So, to get the 'count' you could simply write sizeof($arr, 1); and it should return 13.

I recognize the value of writing your own function, but doesn't this built-in PHP method solve the problem quite succinctly?

Comments

Your Answer

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