RE: Sizeof a multi-dimensional array??
| From: | Tim Ward | Date: | Fri, 27 Apr 2001 11:57:02 +0000 |
| Subject: | RE: Sizeof a multi-dimensional array?? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-50597@lists.php.net to get a copy of this message | ||
you need to count all the elements in each of the elements. $x is a simple
array each member of which is an array
<?php
function ArraySize($Array)
{ $count = 0;
foreach($Array as $Element)
{ if (is_array($Element))
{ $count += ArraySize($Element);
} else
{ $count++;
}
}
return $count;
}
?>
this should work for any array, although for some reason it falls over if
the array passed in hasn't been predfined as an array.
i.e.
<?php
$fred = array();
$fred[1][1][1] = 5;
$fred[1][2][3] = 5;
$fred[2][2][3] = 5;
echo(ArraySize($fred));
?>
... works
but ...
<?php
$fred[1][1][1] = 5;
$fred[1][2][3] = 5;
$fred[2][2][3] = 5;
echo(ArraySize($fred));
?>
... doesn't. I haven't worked out why, yet.
Tim Ward
Senior Systems Engineer
Please refer to the following disclaimer in respect of this message:
http://www.stivesdirect.com/e-mail-disclaimer.html
> -----Original Message-----
> From: Chris Anderson [mailto:chrisderson@ameritech.net]
> Sent: 25 April 2001 01:31
> To: PHP
> Subject: Sizeof a multi-dimensional array??
>
>
> Alright, count($x) would give me the number of array elements
> in $x. But how can I get the count of a multi-dimensional
> array? Count($x[0]) doesn't seem to work. Thanks in advance
>
> Chris Anderson aka "Null"
> --------------------------------------------
> PHP Developer / Nulltech
> PHP-GTK Tester / gtk.php.net
> STA Administrator / www.stronger.org
> DOD Co-Owner / www.dayofdefeat.com
>
>