Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to write a function which counts array elements recursively.

But result is false.

What could it be problem?

$schema = array(
    'div' => array(
        'class' => 'lines',
        'div' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Soap'
        ),
        'layer' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Ball'
            )
        )
);

function count_r($array, $i = 0){
    foreach($array as $k){
        if(is_array($k)){ $i += count_r($k, count($k)); }
        else{ $i++; }
    }
    return $i;
}

echo count_r($schema);
share|improve this question
 
False or zero? Run var_dump($schema) and tell us the output. –  Connor Peet Aug 25 '13 at 14:24
 
@ConnorPeet It returns 14, this is false result –  Deniz Porsuk Aug 25 '13 at 14:24
 
Ah, that is quite different. Why do you have count_r($k, count($k));? Shouldn't need the second argument. –  Connor Peet Aug 25 '13 at 14:26
 
But without this argument how Can I count recursively? –  Deniz Porsuk Aug 25 '13 at 14:28
 
It's hard to explain recursive functions... I tested it, you actually need to do count_r($k, 1); to account for the current array, then it works. I'll put it in an answer. –  Connor Peet Aug 25 '13 at 14:33
add comment

4 Answers

up vote 0 down vote accepted
$schema = array(
    'div' => array(
        'class' => 'lines',
        'div' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Soap'
        ),
        'layer' => array(
             'span' => array(
                'key' => 'Product Name'
            ),
            'val' => 'Ball'
            )
        )
);

function count_r($array, $i = 0){
    foreach($array as $k){
        if(is_array($k)){ $i += count_r($k, 1); }
        else{ $i++; }
    }
    return $i;
}

echo count_r($schema);

This is tested and works correctly.

share|improve this answer
 
This works perfect.. I did not understand the logic of 1 can you explain? –  Deniz Porsuk Aug 25 '13 at 14:41
 
Basically, as you're adding the count_r of that array to the current level, you don't need to factor in the count in the second argument - you'd basically be adding it twice. You need the "1" in there, however, to count the array itself. If you'd want to count just the elements and not the arrays, you would just make the "1" a "0". –  Connor Peet Aug 25 '13 at 14:43
add comment

your function could be simple as

function count_r($array, $i = 0){
    foreach($array as $k){
        $i++;
        if(is_array($k)){ 
            $i += count_r($k); 
        }
    }
    return $i;
}
share|improve this answer
1  
This is the smallest code.. Better than ours. –  Deniz Porsuk Aug 25 '13 at 14:55
add comment

I have altered you code.. Please check it. It is working fine.

$schema = array(
'div' => array(
    'class' => 'lines',
    'div' => array(
         'span' => array(
            'key' => 'Product Name'
        ),
        'val' => 'Soap'
    ),
    'layer' => array(
         'span' => array(
            'key' => 'Product Name'
        ),
        'val' => 'Ball'
        )
    )

);

function count_r($array){
 foreach($array as $k){
    if(is_array($k)){
            $i += count_r($k); 
        }
    else{
        $i++; 
    }
}
return $i;

} echo count_r($schema);

share|improve this answer
 
It returns error, Undefined variable: i –  Deniz Porsuk Aug 25 '13 at 14:45
 
When I put $i = 0 it works too. –  Deniz Porsuk Aug 25 '13 at 14:49
add comment

This is the second result on Google so I'm adding this as a reference. There is already a built-in function in PHP that you can use: count().

You can use it like this:

count($schema, 1);

This function can also detect recursion to avoid infinite loops so it's safer than solutions in other answers.

share|improve this answer
add comment

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.