php 中不能使用对象作为数组吗?

发布于 2024-10-29 08:09:08 字数 502 浏览 2 评论 0原文

从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

究竟谁懂我的在乎 2024-11-05 08:09:08

我也犯了同样的错误。我刚刚通过使用其他变量重命名 $row 来纠正错误。

这是我所做的:

$objReader = PHPExcel_IOFactory::createReaderForFile(ABSPATH.$row['file_path']);
$objReader->setReadDataOnly(true);
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load(ABSPATH.$row['file_path']);

$objPHPExcel = PHPExcel_IOFactory::load(ABSPATH.$row['file_path']);
$objWorksheet = $objPHPExcel->getActiveSheet();

foreach ($objWorksheet->getRowIterator() as $rowsheet) {
    $cellIterator = $rowsheet->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false); 
    foreach ($cellIterator as $cell) {
        $headarray[]= $cell->getValue();
    }
}

注意我已经更改

foreach ($objWorksheet->getRowIterator() as $row) {
                      $cellIterator = $row->getCellIterator();
}

foreach ($objWorksheet->getRowIterator() as $rowsheet) {
                      $cellIterator = $rowsheet->getCellIterator();
}

并且它的工作方式就像一个魅力。

I was making the same mistake. I just corrected the mistake by renaming $row with some other variable.

Here is what I have done:

$objReader = PHPExcel_IOFactory::createReaderForFile(ABSPATH.$row['file_path']);
$objReader->setReadDataOnly(true);
$objReader->setReadFilter( new MyReadFilter() );
$objPHPExcel = $objReader->load(ABSPATH.$row['file_path']);

$objPHPExcel = PHPExcel_IOFactory::load(ABSPATH.$row['file_path']);
$objWorksheet = $objPHPExcel->getActiveSheet();

foreach ($objWorksheet->getRowIterator() as $rowsheet) {
    $cellIterator = $rowsheet->getCellIterator();
    $cellIterator->setIterateOnlyExistingCells(false); 
    foreach ($cellIterator as $cell) {
        $headarray[]= $cell->getValue();
    }
}

Notice i have changed

foreach ($objWorksheet->getRowIterator() as $row) {
                      $cellIterator = $row->getCellIterator();
}

to

foreach ($objWorksheet->getRowIterator() as $rowsheet) {
                      $cellIterator = $rowsheet->getCellIterator();
}

and it's working like a charm.

野却迷人 2024-11-05 08:09:08

$row 是一个对象,因此不能以这种方式设置属性,除非它实现 ArrayAccess。你的问题让我认为 $row 只是一个未使用的变量,在这种情况下,你可以取消设置它和/或使其成为像 $row 这样的数组=数组();

$row is an object, so you cannot set properties this way unless it implements ArrayAccess. Your question makes me think $row is just an unused variable at that point, in which case you can unset that and/or make it an array like $row = array();.

晨曦慕雪 2024-11-05 08:09:08

您已经在脚本的前面部分将 $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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文