Solved

Size of multi-dimensional array

Posted on 2006-06-28
4
2,988 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Form Processing in PHP 11 31
Whether to store ID's or text values for detailed information 3 20
Sql query on a varchar that is numeric. 8 26
html input type 3 21
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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 …

911 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

20 Experts available now in Live!

Get 1:1 Help Now