php 不能将标量值用作数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7681793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot use a scalar value as an array
提问by user947462
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
但我明白了 warning: Cannot use a scalar value as an array
and the echos do nothing. Why ? and what is the solution ?
而回声什么也不做。为什么 ?解决办法是什么?
回答by NullUserException
Here's what you thinkyou want to do. Replace your //problem here
line with:
这就是你认为你想要做的。将您的//problem here
线路替换为:
${'arr' . $x}[] = $todas[$x][$i]."\n";
But I would strongly recommend against doing that. Just use your bidimensional array.
但我强烈建议不要这样做。只需使用您的二维数组。
回答by Paul
I think you meant: ${'arr'.$x}[]
instead of $arr.$x[]
.
我想你的意思是:${'arr'.$x}[]
而不是$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[]
将 $arr 和 $x 的字符串表示连接在一起,所以你最终会得到类似的东西'Array0'[] = ...
而不是$arr0[]
回答by crazyjul
When you write $arr.$x[]
, it is equal to $arr[$x][]
当你写的时候$arr.$x[]
,它等于$arr[$x][]
Try replacing your echos by
尝试通过以下方式替换您的回声
echo $arr[0][0];
echo $arr[1][0];