多维数组的递归计数元素

I try to make recursive function to count elements on array "levels". But can't do that for two hours already. Check example array:

Array ( 
    [0] => Array ( 
        [0] => Array ( 
            [0] => Array ( ) 
            [1] => Array ( ) 
        ) 
        [1] => Array ( ) 
    ) 
    [1] => Array ( 
        [0] => Array (
            [0] => Array (
                [0] => Array ( )
                [1] => Array ( )
            )
        )
    ) 
)

The resulting array that count elements on different levels will be:

Array ([0] => 2, [1] => 3, [2] => 3, [3] => 2)

I made function for count total array elements, but no idea how to count each "level"

function countTotalArr($arr, $lvl) {
    if ($lvl != 0) $cnt = 1; 
    else $cnt = 0; // don't count zero level 

    for ($i = 0; $i < count($arr); $i++)
        $cnt += countArr($arr[$i], $lvl + 1);

    return $cnt;
}

$total = countTotalArr($referralsCount, 0);

Another solution using while:

// $array is your array at the beginning of iteration

$depthMap = [];
$currentDepth = 0;
while(true) {
    $depthMap[$currentDepth] = count($array);

    $carry = [];
    foreach($array as $item) {
        if(is_array($item)) {
            $carry = array_merge($carry, $item);
        }
    }

    if(count($carry) < 1) {
        break;
    }

    $array = $carry;
    $currentDepth++;
}

Try this code:

<?php

$array = Array ( 
    0 => Array ( 
        0 => Array ( 
            0 => Array ( ) ,
            1 => Array ( ) ,
        ) ,
        1 => Array ( ) ,
    ) ,
    1 => Array ( 
        0 => Array (
            0 => Array (
                0 => Array ( ),
                1 => Array ( ),
            ),
        ),
    ) ,
);

function countTotalArr($arr, $lvl) 
{
    $result = array();
    $countOnLevel = count($arr);

    $result[$lvl] = $countOnLevel;

    $tempArray = array();
    foreach($arr as $index => $singleArray)
    {
        foreach($singleArray as $singleSubArray)
        if(is_array($singleSubArray))
            $tempArray[] = $singleSubArray;
    }

    if(!empty($tempArray))
    {
        $levelTemp = $lvl + 1;
        $result = array_merge($result, countTotalArr($tempArray, $levelTemp));
    }

    return $result;
}

$total = countTotalArr($array, 0);

echo '<pre>';
print_r($total);

Result of print_r($total) is:

Array
(
    [0] => 2
    [1] => 3
    [2] => 3
    [3] => 2
)