• Status: Solved
• Priority: Medium
• Security: Public
• Views: 3003

# Size of multi-dimensional array

I know this is probably easy, but I haven't found the answer yet - I need to loop thru a 2-d array.

array(0, array('a','b','c','d','e'), 1, array('f','g','h','i','j'), 2, array('k','l','m','n','o'))

Is there an easy way to find out that my array is 3x5?  I need to know the dimensions because I need to group the data together (a-e, f-j, and k-o) for display purposes.
0
• 2
1 Solution

Commented:
well, count(\$array) will return the size of the 1D part.  You could do count(\$array[0]) to find the 2D part.  That will work fine if you know all the elements have the same 2D array size.

but if you do need to loop through the entire array:

\$arr=array(array('a','b','c','d','e'), array('f','g','h','i','j'), array('k','l','m','n','o'));

for (\$a=0;\$a<count(\$arr);\$a++)
for (\$b=0;\$b<count(\$arr[\$a]);\$b++)
echo \$arr[\$a][\$b];

-Matt
0

Author Commented:
Thanks Matt!
0

Senior DeveloperCommented:
array(0, array('a','b','c','d','e'), 1, array('f','g','h','i','j'), 2, array('k','l','m','n','o'))

array(array('a','b','c','d','e'), array('f','g','h','i','j'), array('k','l','m','n','o'))

so, to loop through 2-d array :

\$arr = array(array('a','b','c','d','e'), array('f','g','h','i','j'), array('k','l','m','n','o'));
for (\$i = 0; \$i < count(\$arr); \$i++)
{
for (\$j = 0; \$j < count(\$arr[\$i]); \$j++)
{
echo ' [' . \$i . '][' . \$j . '] = ' . \$arr[\$i][\$j];
}
echo "<br>";
}

Bonmat86.
0

Senior DeveloperCommented:
oops, sorry...

Bonmat86.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.