Solved

Size of multi-dimensional array

Posted on 2006-06-28
4
2,991 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
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 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