0

I am trying to generate an MS Excel spread sheet using PHPExcel 1.7.6 . I am having trouble determining the structure of the array expected.

The code that builds up the columns and rows is as follows:

function _headers() { 
        $i=0; 
        foreach ($this->data[0] as $field => $value) { 
            if (!in_array($field,$this->blacklist)) { 
                $columnName = Inflector::humanize($field); 
                $this->sheet->setCellValueByColumnAndRow($i++, 4, $columnName); 
            } 
        } 
        $this->sheet->getStyle('A4')->getFont()->setBold(true); 
        $this->sheet->getStyle('A4')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID); 
        $this->sheet->getStyle('A4')->getFill()->getStartColor()->setRGB('969696'); 
        $this->sheet->duplicateStyle( $this->sheet->getStyle('A4'), 'B4:'.$this->sheet->getHighestColumn().'4'); 
        for ($j=1; $j<$i; $j++) { 
            $this->sheet->getColumnDimension(PHPExcel_Cell::stringFromColumnIndex($j))->setAutoSize(true); 
        } 
    } 

    function _rows() { 
        $i=5; 
        foreach ($this->data as $row) { 
            $j=0; 
            foreach ($row as $field => $value) { 
                if(!in_array($field,$this->blacklist)) { 
                    $this->sheet->setCellValueByColumnAndRow($j++,$i, $value); 
                } 
            } 
            $i++; 
        } 
    } 

I'm currently getting an 'Invalid argument supplied for foreach()' error.

I would appreciate it if somebody can outline the correct array structure required.

1
  • 1
    Your error message suggests that $this->data[0] is not an array, begin debugging by trying print_r($this->data[0]) Commented Feb 15, 2012 at 17:22

1 Answer 1

1

As IsisCode said, it sounds like you're looking in the wrong direction by associating the problem with PHPExcel. That error is generally just saying that the first argument supplied to foreach() isn't an array.

I don't see anything indicating that the problem is explicitly with the initial foreach in the _headers() method though. If there's a non array element in $this->data then your _rows() method could produce the error as well.

Given:

$this->data = Array( 
    Array('foo' => 'bar'),
    'baz'
)

That would cause the second foreach in _rows() to fail, as an example. Your error message should be able to point you to which foreach() is failing, but ultimately it sounds like you've got a non-array element in $this->data where you don't expect it. If that can't be helped, then consider verifying you're dealing with an array before calling foreach:

    function _rows() { 
    $i=5; 
    foreach ($this->data as $row) { 
        $j=0;
        if(!is_array($row)) { continue; }  // Ignore non-array elements
        foreach ($row as $field => $value) { 
            if(!in_array($field,$this->blacklist)) { 
                $this->sheet->setCellValueByColumnAndRow($j++,$i, $value); 
            } 
        } 
        $i++; 
    } 

Verifying the type of a variable before handing it to a type-specific function is never a bad idea and can save a lot of headache in general.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.