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);
var_dump($schema)
and tell us the output. – Connor Peet Aug 25 '13 at 14:24count_r($k, count($k));
? Shouldn't need the second argument. – Connor Peet Aug 25 '13 at 14:26count_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