Home » excel » cannot use object as array in php?

cannot use object as array in php?

Posted by: admin May 14, 2020 Leave a comment


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


$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

How to&Answers:

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->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();
    foreach ($cellIterator as $cell) {
        $headarray[]= $cell->getValue();

Notice i have changed

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


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

and it’s working like a charm.


$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();.


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.