Assigning Values to a Multidimensional Array

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

Post Reply
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Assigning Values to a Multidimensional Array

Post by webcan »

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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

$myarray[$counter][$i] = ('123', $value2);
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

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)
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

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 »

:( 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.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

the way i said it makes it an element of $counter[]
http://www.php.net/manual/en/language.types.array.php
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

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 »

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 »

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 »

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 »

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 »

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.
Post Reply