Page 1 of 1

Multidimensional Array Question

Posted: Fri Apr 22, 2005 10:56 pm
by Texan
I have an array:

Code: Select all

$var = array('test' => 
             array('sub1', 'sub2', 'sub3'),
             'test1' =>
             array('sub1', 'sub2'),
             'test2');
I know how to get the size of a regular array, but how do I get
the size of the sub array? i.e: how do I get that the sub array
of $var[test] is equal to 3 and that the size of the sub array
$var[test1] is equal to 2?

Thanks

Posted: Sat Apr 23, 2005 8:49 am
by Chris Corbyn
Just the same way you do with any other array...

Code: Select all

//Enitre array
$entire = count($var);

//Sub array 1
$sub1 = count($var['test']);

//Sub array 2
$sub2 = count($var['test1']);

Posted: Sat Apr 23, 2005 10:00 am
by wmasterj
yes...

and then you use a foreach loop. If you want to be able to have even deeper levels of arrays to be tested i suggest you script a function for it.

Posted: Sat Apr 23, 2005 11:30 am
by Chris Corbyn
No need for any such foreach loop to recursively count.

If you read the documentation count() can do this.

i.e.

Code: Select all

$number = count($array, 1); //Or count($array, COUNT_RECURSIVE);