Jump to content


Photo

Question About Arrays


  • Please log in to reply
2 replies to this topic

#1 judeddokoinu

judeddokoinu
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 03 May 2006 - 10:15 PM

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.

for($i=0;$i<$somenum;$i++)
{
//do something to get the year
$myarray[$i] = $year;
}
^-- Results in --v
Array ( [0] => 1995 [1] => 1997 [2] => 2002 [3] => 2003 [4] => 2004 [5] => 2005 [6] => 2006 )

Then, later in the program, I revisit the array, and attempt to set another dimension...
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;
}

Let's say that I set $day = 09... When I try to set the day? $myarray[$x][$y][$i] = $day; ... I get:

Array ( [0] => 0995 [1] => 1997 [2] => 2002 [3] => 2003 [4] => 2004 [5] => 2005 [6] => 2006 )

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!

#2 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 04 May 2006 - 01:21 AM

when you want to add a new dimension to your array you need to put something like
$myarray[$x] = array();
$myarray[$x][$i] = "blah";


#3 judeddokoinu

judeddokoinu
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 04 May 2006 - 05:23 AM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users