Solved

# Size of multi-dimensional array

Posted on 2006-06-28
2,994 Views
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
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 6

Accepted Solution

mattjp88 earned 125 total points
ID: 17006839
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

LVL 1

Author Comment

ID: 17006863
Thanks Matt!
0

LVL 10

Expert Comment

ID: 17006913
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

LVL 10

Expert Comment

ID: 17006918
oops, sorry...

Bonmat86.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called \$myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
###### Suggested Courses
Course of the Month7 days, 18 hours left to enroll