Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use a scalar value as an array

I am trying 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 echos do nothing. Why ? and what is the solution ?

like image 878
user947462 Avatar asked Oct 06 '11 23:10

user947462


3 Answers

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

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

But I would strongly recommend against doing that. Just use your bidimensional array.

like image 148
NullUserException Avatar answered Oct 19 '22 23:10

NullUserException


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

 $arr.$x[]

Will concatenate the string representation of $arr and $x together so you end up with something like 'Array0'[] = ... instead of $arr0[]

like image 25
Paul Avatar answered Oct 19 '22 23:10

Paul


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

Try replacing your echos by

echo $arr[0][0];
echo $arr[1][0];
like image 38
crazyjul Avatar answered Oct 20 '22 01:10

crazyjul