Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Size of multi-dimensional array

Posted on 2006-06-28
4
Medium Priority
?
2,996 Views
Last Modified: 2011-10-03
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
Comment
Question by:BigMonkeyHead
[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
  • Learn & ask questions
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
mattjp88 earned 500 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

by:BigMonkeyHead
ID: 17006863
Thanks Matt!
0
 
LVL 10

Expert Comment

by:Khanh Doan
ID: 17006913
array(0, array('a','b','c','d','e'), 1, array('f','g','h','i','j'), 2, array('k','l','m','n','o'))

your array should be

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

by:Khanh Doan
ID: 17006918
oops, sorry...

Bonmat86.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question