Jump to content

Archived

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

judeddokoinu

Question About Arrays

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.