PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Wed Apr 14, 2004 12:47 pm
Hello:
I am trying to assign values to a multi-dimensional array, but it's not working out.
I have a loop that has variable, $counter[$i], which increments by 1 everytime it goes through the loop.
I'm doing this:
$myarray[$counter[$i]] = ('123', $value2);
And it's not working.
Can anyone tell me what I can do to fix this?
Thanks,
Peter.
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Wed Apr 14, 2004 1:03 pm
I don't think that's what I mean to do.
Let's say that $counter[$i] = 1, what I want is
$myarray[1] = ...
($i is not supposed to be a sub element of $myarray, it is an element of $counter)
PrObLeM
Forum Contributor
Posts: 418 Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:
Post
by PrObLeM » Wed Apr 14, 2004 1:07 pm
Hopefully this helps
Code: Select all
for($x=$counter[$i]; $x<$stopnumber; $x++)
{
$myarray[$x] = ('123', $value2);
}
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Wed Apr 14, 2004 1:09 pm
Unfortuantely the counter doesnt increment by 1. It relies on another piece of data for the incrementing number, so I could do a modified form of what you just sent, but it would be complicated.
I was hoping there was an easy way to incorporate that other array in the reference.
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Wed Apr 14, 2004 1:21 pm
OK, then that might work, but i have a second similar statement in that line which refers to a specific element of the $counter array, so something like:
$myarray[$counter][1] (I'm trying to refer to $counter[1])
But that comes back with illegal offset.
Thanks,
Peter.
magicrobotmonkey
Forum Regular
Posts: 888 Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA
Post
by magicrobotmonkey » Wed Apr 14, 2004 1:27 pm
in $myarray or in $counter and are you sure you have that index set?
also, don't forget that arrays start at 0
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Wed Apr 14, 2004 1:35 pm
Yes. Because if i do echo $counter[1] it comes back with a value.
But $myarray[$counter][1] doesn't work.
magicrobotmonkey
Forum Regular
Posts: 888 Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA
Post
by magicrobotmonkey » Wed Apr 14, 2004 1:39 pm
hmm.. weird - read that link i sent from the php manual that should help you
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Wed Apr 14, 2004 1:44 pm
OK, I will try.
Thank you!
Peter.
webcan
Forum Commoner
Posts: 66 Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada
Post
by webcan » Wed Apr 14, 2004 2:09 pm
OK, I fixed the problem. If anyone has this issue, it's because I didn't put array after the equal sign.
$myarray[$counter[1]] = array('123', $value2);
That statement works.