I’m seeing some odd behavior with count( $arr, COUNT_RECURSIVE ) when used with SplFixedArray. Take this block of code, for instance…
$structure = new SplFixedArray( 10 );
for( $r = 0; $r < 10; $r++ )
{
$structure[ $r ] = new SplFixedArray( 10 );
for( $c = 0; $c < 10; $c++ )
{
$structure[ $r ][ $c ] = true;
}
}
echo count( $structure, COUNT_RECURSIVE );
Result…
> 10
You would expect a result of 110. Is this normal behavior due to the fact that I’m nesting SplFixedArray objects?
SplFixedArrayimplementsCountable, butCountabledoes not allow for a arguments, hence you cannot count recursive. The argument is ignored. You can see this from the method signature ofSplFixedArray::countandCountable::count.There is a Feature Request open for this at https://bugs.php.net/bug.php?id=58102
You can sublass
SplFixedArrayand make it implementRecursiveIteratorand then overload thecountmethod to useiterate_countbut then it will always count all the elements, e.g. it’s alwaysCOUNT_RECURSIVEthen. Can also add a dedicated method though.demo