Jump to content

Question About Arrays


judeddokoinu

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.