Page 1 of 1

Assigning Values to a Multidimensional Array

Posted: Wed Apr 14, 2004 12:47 pm
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.

Posted: Wed Apr 14, 2004 12:57 pm
by magicrobotmonkey
$myarray[$counter][$i] = ('123', $value2);

Posted: Wed Apr 14, 2004 1:03 pm
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)

Posted: Wed Apr 14, 2004 1:07 pm
by PrObLeM
Hopefully this helps

Code: Select all

for($x=$counter[$i]; $x<$stopnumber; $x++)
{
$myarray[$x] = ('123', $value2);
}

Posted: Wed Apr 14, 2004 1:09 pm
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.

Posted: Wed Apr 14, 2004 1:15 pm
by magicrobotmonkey
the way i said it makes it an element of $counter[]
http://www.php.net/manual/en/language.types.array.php

Posted: Wed Apr 14, 2004 1:21 pm
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.

Posted: Wed Apr 14, 2004 1:27 pm
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

Posted: Wed Apr 14, 2004 1:35 pm
by webcan
Yes. Because if i do echo $counter[1] it comes back with a value.

But $myarray[$counter][1] doesn't work.

Posted: Wed Apr 14, 2004 1:39 pm
by magicrobotmonkey
hmm.. weird - read that link i sent from the php manual that should help you

Posted: Wed Apr 14, 2004 1:44 pm
by webcan
OK, I will try.

Thank you!
Peter.

Posted: Wed Apr 14, 2004 2:09 pm
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.