note 80352 deleted from function.count by levim
| From: | levim@php.net | Date: | Thu, 10 Jan 2013 14:21:21 +0000 |
| Subject: | note 80352 deleted from function.count by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192877@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
Here is an iterative soloution of danny's count_recursive function which might be more
efficient due to the missing recursion
<?php
/**
* counts elements of an multidimensional array
*
* @param array $array Input Array
* @param int $limit dimensions that shall be considered (-1 means no limit )
* @return int counted elements
*/
function multicount ($array, $limit = -1)
{
$cnt = 0;
$limit = $limit > 0 ? (int) $limit : -1;
$arrs[] = $array;
for ($i=0; isset($arrs[$i]) && is_array($arrs[$i]); ++$i)
{
foreach ($arrs[$i] as $value)
{
if (!is_array($value) ) ++$cnt;
elseif( $limit==-1 || $limit>1 )
{
if( $limit>1 ) --$limit;
$arrs[] = $value;
}
}
}
return $cnt;
}
?>