Solved

Size of multi-dimensional array

Posted on 2006-06-28
4
2,986 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
Comment Utility
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
Comment Utility
Thanks Matt!
0
 
LVL 10

Expert Comment

by:Khanh Doan
Comment Utility
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
Comment Utility
oops, sorry...

Bonmat86.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now