Page 1 of 1

Can't figure out how to loop through this array!? help!

Posted: Tue Aug 12, 2008 2:57 pm
by matstars
I have information being put into a multidimensional array:

It's sorted by year and classroom

$Students[$year][$classroom]["Gradelevel"]=$Gradelevel;
$Students[$year][$classroom]["Classroom"]=$Classroom;
$Students[$year][$classroom]["Name"]=$Name;
$Students[$year][$classroom]["DOB"]=$DOB;
$Students[$year][$classroom]["ParentName"]=$ParentsName;

How can I look through both arrays?

I tried this:

foreach($students as $info => $value)
{
echo "<BR><BR>$info is $value<BR><BR>";
}

it returns:

2005 is array
2006 is array
2007 is array


I want it to return :

$Students[$year][$classroom]["Gradelevel"]=$Gradelevel;
$Students[$year][$classroom]["Classroom"]=$Classroom;
$Students[$year][$classroom]["Name"]=$Name;
$Students[$year][$classroom]["DOB"]=$DOB;
$Students[$year][$classroom]["ParentName"]=$ParentsName;

2005 gradelevel is 1
2005 classroom is 115
2005 Name is Mike Jonas
2005 DOB is 20030519
2005 ParentName is Jane Jonas

2006 gradelevel is 2
2006 classroom is 143
2006 Name is Mike Jonas
2006 DOB is 20030519
2006 ParentName is Jane Jonas


2005 gradelevel is 4
2005 classroom is 326
2005 Name is Matt Wong
2005 DOB is 20001224
2005 ParentName is Jong Wong

2005 gradelevel is 5
2005 classroom is 324
2005 Name is Matt Wong
2005 DOB is 20001224
2005 ParentName is Jong Wong

etc.etc.

Little help:)

Re: Can't figure out how to loop through this array!? help!

Posted: Tue Aug 12, 2008 3:24 pm
by filippo.toso
You can use a code like the following:

Code: Select all

foreach ($Students as $year => $classroomarray) {
    foreach ($classroomarray as $classroom => $field) {
        foreach ($field as $key => $value) {
            echo("$year $key is $value <br />");
        }
    }
}