radi8 Posted February 19, 2010 Share Posted February 19, 2010 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. Quote Link to comment Share on other sites More sharing options...
Goat Posted February 19, 2010 Share Posted February 19, 2010 How does the db table look like? In php multidimensional arrays are essentially arrays within arrays, so you have to init every subarray by using array keyword. Goat Quote Link to comment Share on other sites More sharing options...
teamatomic Posted February 19, 2010 Share Posted February 19, 2010 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 Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 19, 2010 Share Posted February 19, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.