Jump to content

Working with multidimensional array


radi8

Recommended Posts

I am trying to build a simple array but am having problems getting it to work correctly. In essence, I need to build an array that has the following structure:

Year

      Month

            Details

            ...

            15 entries here

 

What I would like it to be an array of type: arr[0][0][0] and look like is the following:

2007

      1

                data1

                data2

                ...

                data15

      2

                data1

                data2

                ...

                data15

...

2008

      12

                data1

                data2

                ...

                data15

 

and so on...

 

The issue is in trying to set the first (year) and second  (month) element of the array (see code below).

if($row = mysql_fetch_array($result))
{
     $i=0;
     $j=0;
//this next line works, but subsequent lines fail
     $arr[$i]=$row[2];
     $arr[$i][$j]=$row[3];     
     $arr[$i][$j][0]=$row[4];
     $arr[$i][$j][1]=$row[5];
     $arr[$i][$j][2]=$row[4];
     $arr[$i][$j][3]=$row[5];
}

 

I know that I am doing something basic wrong, but need a push in the right direction.

Link to comment
https://forums.phpfreaks.com/topic/192675-working-with-multidimensional-array/
Share on other sites

what I think you want would look like this

$array[2010][january][detail]=

$array[2010][february][detail]=

...

$array[2010][december][detail]=

 

what you would end up with is an array of the year that would contain 12 arrays, one for each month containing an array detail that would have as many elements as you fed into it.

 

You could alternatively substitute 1-12 for the month name. But either way you could use vars obtained from the date() function to easily access or add to the array.

 

 

 

HTH

Teamatomic

The problem is that on this line

     $arr[$i]=$row[2];

You are defining the value of $arr[$i] as a string. But,then on this line

     $arr[$i][$j]=$row[3];

you are trying to define an array value for $arr[$i]. You can't have $arr[$i] be a string and an array.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.