Denix
Denix

Reputation: 522

Php recursive array counting

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);

Upvotes: 12

Views: 19812

Answers (7)

Christian P
Christian P

Reputation: 12240

There is a built-in function in PHP that you can use: count().

This is how you can use it:

count($schema, COUNT_RECURSIVE);

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

Upvotes: 45

soma
soma

Reputation: 186

count($array, COUNT_RECURSIVE)-count($array)

Upvotes: 0

Connor Peet
Connor Peet

Reputation: 6265

Transferred from comment below answer:

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".

$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.

Upvotes: 1

stubben
stubben

Reputation: 321

If you want to count specific keys/values you can use the built-in array_walk_recursive with a closure function.

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

$elements = 0;
array_walk_recursive($schema, function($value, $key) use (&$elements) {
  if (strstr($value, 'Product')) $elements++;
});

print $elements; // 2

/*
Values
  lines
  Product Name
  Soap
  Product Name
  Ball

Keys
  class
  key
  val
  key
  val
*/

Upvotes: 1

Sorin Gheata
Sorin Gheata

Reputation: 329

PHP has a built-in method for this purpose, called count(). If passed without any additional parameters, count() returns the number of array keys on the first level. But, if you pass a second parameter to the count method count( $schema, true ), then the result will be the total number of keys in the multi-dimensional array. The response marked as correct only iterates first and second level arrays, if you have a third child in that array, it will not return the correct answer.

This could be written as a recursive function, if you really want to write your own count() method, though.

Upvotes: 14

Manivasagan
Manivasagan

Reputation: 212

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);

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27845

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;
}

Upvotes: 1

Related Questions