php 中不能使用对象作为数组吗?
从 excel 读取数据
$row['name'] = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(3, $l)->getCalculatedValue();
我试图通过此代码var_dump returns string(4) "CCC3"
,但我有一个奇怪的问题
代码
$row['name'] =
返回 Cannot use object of type PHPExcel_Worksheet_Row as array 如果我使用简单的
$row_name =
就可以了......
读取数据我使用 phpExcel。
当然,我可以将其更改为第二个选项,但我不知道为什么它不起作用,
感谢帮助
I trying to read data from excel by this code
$row['name'] = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(3, $l)->getCalculatedValue();
var_dump returns string(4) "CCC3"
but i have a strange problem
code
$row['name'] =
returns Cannot use object of type PHPExcel_Worksheet_Row as array
If I use simple
$row_name =
its ok....
to read data Im using phpExcel.
Of course, I can change it to second option, but I dont know, why its not working
thanks for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也犯了同样的错误。我刚刚通过使用其他变量重命名
$row
来纠正错误。这是我所做的:
注意我已经更改
为
并且它的工作方式就像一个魅力。
I was making the same mistake. I just corrected the mistake by renaming
$row
with some other variable.Here is what I have done:
Notice i have changed
to
and it's working like a charm.
$row
是一个对象,因此不能以这种方式设置属性,除非它实现ArrayAccess
。你的问题让我认为$row
只是一个未使用的变量,在这种情况下,你可以取消设置
它和/或使其成为像$row 这样的数组=数组();
。$row
is an object, so you cannot set properties this way unless it implementsArrayAccess
. Your question makes me think$row
is just an unused variable at that point, in which case you canunset
that and/or make it an array like$row = array();
.您已经在脚本的前面部分将
$row
的值设置为 PHPExcel_Worksheet_Row 对象(可能使用了 PHPExcel 的行迭代器),因此$row
已经作为对象存在。这意味着当您将值指定为$row['name']
时,不能简单地依赖 PHP 将其重新定义为关联数组。使用以前未使用过的另一个变量名称(例如 $rowdata),或者在尝试为其赋值之前
unset($row);
。You've already set the value of
$row
as a PHPExcel_Worksheet_Row object earlier in your script, perhaps using PHPExcel's row iterator, so$row
already exists as an object. This means that you can't simply rely on PHP to redefine it as an associative array when you assign a value to it as$row['name']
.Either use another variable name (e.g. $rowdata) that hasn't previous been used, or
unset($row);
before trying to assign a value to it.