judeddokoinu Posted May 3, 2006 Share Posted May 3, 2006 I have an array that I create early in my PHP script as a single dimensional array. It contains integers related to years that it gathers from input.Later on in the script, I decided that I'd like to add onto this array with a dimension for months, and a dimension for days. I'll also (later) be adding another dimension containing the data I need.But the problem I'm having is that the first dimension's data is getting overwritten when I attempt to set the second or third dimension's value.[code]for($i=0;$i<$somenum;$i++){//do something to get the year$myarray[$i] = $year;}[/code]^-- Results in --v[code]Array ( [0] => 1995 [1] => 1997 [2] => 2002 [3] => 2003 [4] => 2004 [5] => 2005 [6] => 2006 )[/code]Then, later in the program, I revisit the array, and attempt to set another dimension...[code]for($i=0;$i<$somenum;$i++){//do something to get the month$myarray[$x][$i] = $month;}for($i=0;$i<$somenum;$i++){//do something to get the day$myarray[$x][$y][$i] = $day;}[/code]Let's say that I set $day = 09... When I try to set the day? $myarray[$x][$y][$i] = $day; ... I get:[code]Array ( [0] => 0995 [1] => 1997 [2] => 2002 [3] => 2003 [4] => 2004 [5] => 2005 [6] => 2006 )[/code]The year in the [0] position gets it's first two numbers overwritten! And there are no extra dimensions!So... How can I add these dimensions? I'm so stumped! Link to comment https://forums.phpfreaks.com/topic/9012-question-about-arrays/ Share on other sites More sharing options...
KrisNz Posted May 4, 2006 Share Posted May 4, 2006 when you want to add a new dimension to your array you need to put something like[code]$myarray[$x] = array();$myarray[$x][$i] = "blah";[/code] Link to comment https://forums.phpfreaks.com/topic/9012-question-about-arrays/#findComment-33172 Share on other sites More sharing options...
judeddokoinu Posted May 4, 2006 Author Share Posted May 4, 2006 I tried that out, but it wasn't what I was looking for. I finally decided to just use multiple single-dimensional arrays, and it's working fine that way.After reading your post and playing around with the code, I think what I was trying to do was not working - like, could never work, and so I decided to take the easiest detour instead. Thanks for the help, though. Link to comment https://forums.phpfreaks.com/topic/9012-question-about-arrays/#findComment-33212 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.