PHP 数组结构

发布于 2025-01-06 06:38:37 字数 1498 浏览 0 评论 0原文

我正在尝试使用 PHPExcel 1.7.6 生成 MS Excel 电子表格。我无法确定预期数组的结构。

构建列和行的代码如下:

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++; 
        } 
    } 

我当前收到“为 foreach() 提供的参数无效”错误。

如果有人能概述所需的正确数组结构,我将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

姜生凉生 2025-01-13 06:38:37

正如 IsisCode 所说,听起来您将问题与 PHPExcel 关联起来似乎找错了方向。该错误通常只是说提供给 foreach() 的第一个参数不是数组。

不过,我没有看到任何表明问题明显与 _headers() 方法中的初始 foreach 相关的内容。如果 $this->data 中有非数组元素,那么您的 _rows() 方法也可能会产生错误。

鉴于:

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

例如,这将导致 _rows() 中的第二个 foreach 失败。您的错误消息应该能够指出哪个 foreach() 失败,但最终听起来好像您在 $this->data 中有一个您不希望出现的非数组元素。如果这没有帮助,那么请考虑在调用 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++; 
    } 

在将变量传递给特定于类型的函数之前验证变量的类型绝不是一个坏主意,并且可以在一般的。

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.

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