?
Solved

Size of multi-dimensional array

Posted on 2006-06-28
4
Medium Priority
?
2,998 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

850 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