Get sum of all the data inside an multidimensional array recursively :

401
January 07, 2017, at 6:41 PM

I've this array :

Array
(
    [self] => Folder A
    [my_data] => Array
        (
        )
    [18] => Array
        (
            [self] => Folder AA
            [my_data] => Array
                (
                    [0] => stdClass Object()
                )
            [20] => Array
                (
                    [self] => Folder AA
                    [my_data] => Array
                        (
                            [0] => stdClass Object()
                        )
                    [21] => Array
                )

I want to get the total number of records in of the present in 'my_data'. For this I've created this function that call recursively to add the count :

function getDocumentCount($tab, $count = 0) {
    foreach ($tab as $subTabKey => $subTabValue) {
        if ($subTabKey == 'my_data') {
            $count += count($subTabValue);
        }
        if ( count($tab) > 2 && $subTabKey != 'self' && $subTabKey != 'my_data' ) {
            $count += getDocumentCount($subTabValue, $count);
        }
    }
    return $count;
}

But this function always returning 0. The above example should return 2

Edit : This should return 5 but its returning 9

Array
(
    [self] => Main Folder 22
    [my_data] => Array
        (
        )
    [17] => Array
        (
            [self] => New Sub
            [my_data] => Array
                (
                    [0] => stdClass Object()
                    [1] => stdClass Object()
                )
        )
    [8] => Array
        (
            [self] => sub folder 21
            [my_data] => Array
                (
                )
            [9] => Array
                (
                    [self] => sub folder 211
                    [my_data] => Array
                        (
                            [0] => stdClass Object()
                            [1] => stdClass Object()
                            [2] => stdClass Object()
                        )
                )
        )
)
Answer 1

You just calculated the first dimension of the array which the length of my_data is 0, so the $count is 0. Though you also calculated the inner dimmension of the array, but you didnot sum the output of them. You have to also sum the output of the inner output.

function getDocumentCount($tab, &$count) {
    foreach ($tab as $subTabKey => $subTabValue) {
        if ($subTabKey == 'my_data') {
            $count += count($subTabValue);
        }
        if ( count($tab) > 2 && $subTabKey != 'self' && $subTabKey != 'my_data' ) {
            getDocumentCount($subTabValue, $count);
        }
    }
}

use it with:

$count = 0;
getDocumentCount($tab, $count);
echo $count;
Answer 2

You are not adding the result of the call to getDocumentCount() to your count. Try changing the sixth line of your function to:

$count += getDocumentCount($subTabValue, $count);

Does that fix your problem?

Rent Charter Buses Company
READ ALSO
EC2 Linux server execute php file

EC2 Linux server execute php file

Is it any way to execute php file by address examplecom/some-link but actual file will be out of your public folder

370
data doesn't get inserted into database

data doesn't get inserted into database

my code shows no error ,but when i press submit button my data doesn't get inserted into database

276
php loop is skipping or missing some results of mysql query

php loop is skipping or missing some results of mysql query

php loop is skipping or missing some results of mysql query, the same query is bringing 181 results in PhpMyAdmin and even in sqlYOG where as the loop is bringing only 175

226