I am writing a script that requires a 3 dimension array for storing data. I've read all I can find in 4 manuals, and gone through numerous pages in the forums searching on arrays, but can't seem to find the answer.. It is probably something simple that I'm missing, but I'm missing it..

I wrote a little test script to see what is wrong, and can't even get it to work.. Here is the script.

for ($a=1; $a<3; ++$a) {
	for ($b=1; $b<3; ++$b) {
		for ($c=1; $c<3; ++$c) {
$test[$a][$b][$c] = $a*$b*$c;
print "a is $a and b is $b and c is $c 
SO test[$a][$b][$c] is $test[$a][$b][$c]<br>";
		}
	}
}

What I expected as a result was somethin like this

a is 1 and b is 1 and c is 1 SO test[1][1][1] is 1
a is 1 and b is 1 and c is 2 SO test[1][1][2] is 2
a is 1 and b is 2 and c is 1 SO test[1][2][1] is 2
a is 1 and b is 2 and c is 2 SO test[1][2][2] is 4
a is 2 and b is 1 and c is 1 SO test[2][1][1] is 2
a is 2 and b is 1 and c is 2 SO test[2][1][2] is 4
a is 2 and b is 2 and c is 1 SO test[2][2][1] is 4
a is 2 and b is 2 and c is 2 SO test[2][2][2] is 8

But instead I get this:

a is 1 and b is 1 and c is 1 SO test[1][1][1] is Array[1][1]
a is 1 and b is 1 and c is 2 SO test[1][1][2] is Array[1][2]
a is 1 and b is 2 and c is 1 SO test[1][2][1] is Array[2][1]
a is 1 and b is 2 and c is 2 SO test[1][2][2] is Array[2][2]
a is 2 and b is 1 and c is 1 SO test[2][1][1] is Array[1][1]
a is 2 and b is 1 and c is 2 SO test[2][1][2] is Array[1][2]
a is 2 and b is 2 and c is 1 SO test[2][2][1] is Array[2][1]
a is 2 and b is 2 and c is 2 SO test[2][2][2] is Array[2][2]

Can anyone tell me why?

Thanks, I really appreciate your help. this is stumping me.

doug

    Drop

    $test[$a][$b][$c] = $a$b$c;

    and change the last line within that for loop to

    SO test[$a][$b][$c] is " . ($a$b$c) . "<br>";

    so your whole thing would look like this

    
    for ($a=1; $a<3; ++$a) {
        for ($b=1; $b<3; ++$b) {
            for ($c=1; $c<3; ++$c) {
    
           print "a is $a and b is $b and c is $c 
           SO test[$a][$b][$c] is " . ($a*$b*$c) . "<br>";
    
        }
    }
    }

    Cgraz

      cgraz,

      Thanks for the reply, but that doesn't assign any value to the array elements $test[$a][$b][$c], which is what I'm having a problem with.

      Sorry, I must not have made that very clear.. I need to assign a value to each of the elements in the 3 dim array..

      a is 1 and b is 1 and c is 1 SO test[1][1][1] is 1
      a is 1 and b is 1 and c is 2 SO test[1][1][2] is 2
      a is 1 and b is 2 and c is 1 SO test[1][2][1] is 2
      a is 1 and b is 2 and c is 2 SO test[1][2][2] is 4
      a is 2 and b is 1 and c is 1 SO test[2][1][1] is 2
      a is 2 and b is 1 and c is 2 SO test[2][1][2] is 4
      a is 2 and b is 2 and c is 1 SO test[2][2][1] is 4
      a is 2 and b is 2 and c is 2 SO test[2][2][2] is 8

      Is there a different way to assign a value to each element?

      thanks
      doug

        if you want $test[$a][$b][$c] to equal the factor of $a $b $c, then use the following:

        for ($a=1; $a<3; ++$a) {
            for ($b=1; $b<3; ++$b) {
                for ($c=1; $c<3; ++$c) {
        
               $test[$a][$b][$c] = ($a * $b * $c);
        
               print "a is $a and b is $b and c is $c 
               SO test[$a][$b][$c] is " . $test[$a][$b][$c] . "<br>";
        
            }
        }
        }

        Cgraz

          I see... It wasn't really the assignments that I was having problems with
          (although I forgot to do the assignment in my example)

          The assignments worked fine, but it was when I tried to display them, I was including them within the "string" instead of concatenating them..

          $position[$x+1][$y*2] = ($position [$x][$y]+($level[$x][0][0]));

          Would be a perfectly valid assignment then, I presume.

          Thank you for your help.. I figured it would be something simple, but never gave that a thought.

          doug

            Write a Reply...