You cannot use a scalar value as an array

I am trying to use this code:

for ($x = 0; $x < $numCol; $x++) { for ($i = 0; $i < $numRows; $i++) { $arr.$x[] = $todas[$i][$x]."\n"; //problem here } } echo $arr0[0]; echo $arr1[0]; ... 

But I get this warning: Cannot use a scalar value as an array

and the echo does nothing. What for? and what is the solution?

+8
arrays loops php for-loop
source share
3 answers

That's what you think you want to do. Replace the line //problem here with:

 ${'arr' . $x}[] = $todas[$x][$i]."\n"; 

But I would highly recommend doing this. Just use your two dimensional array.

+11
source share

I think you meant: ${'arr'.$x}[] instead of $arr.$x[] .

  $arr.$x[] 

It will concatenate the string representation of $ arr and $ x together so you get something like 'Array0'[] = ... instead of $arr0[]

+5
source share

When you write $arr.$x[] , it is equal to $arr[$x][]

Try replacing the echo with

 echo $arr[0][0]; echo $arr[1][0]; 
+1
source share

All Articles