doujichan1399 2015-02-17 05:03
浏览 49
已采纳

Wordpress PHP非法字符串错误

I am having some issues with a wordpress plugin (Customer Order CSV Export) that for some reason has started to put error warning into the exported file. It is saying there is an illegal string offset for every section of the order (so about 40 warnings for each file). I have only attached the warning for the order_number but the rest all say the same lines:

<b>Warning</b>:  Illegal string offset 'order_number' in <b>/home/.../wp-content/plugins/woocommerce-customer-order-csv-export/includes/class-wc-customer-order-csv-export-generator.php</b> on line <b>709</b><br />
<b>Warning</b>:  Illegal string offset 'order_number' in <b>/home/.../wp-content/plugins/woocommerce-customer-order-csv-export/includes/class-wc-customer-order-csv-export-generator.php</b> on line <b>713</b><br />
<b>Warning</b>:  Illegal string offset 'order_number' in <b>/home/.../wp-content/plugins/woocommerce-customer-order-csv-export/includes/class-wc-customer-order-csv-export-generator.php</b> on line <b>713</b><br />

The offending lines in the php file look like this:

private function write( $row ) {

    $data = array();

    foreach ( $this->headers as $header_key => $_ ) {

        if ( ! isset( $row[ $header_key ] ) ) {
            $row[ $header_key ] = '';
        }

        // strict string comparison, as values like '0' are valid
        $data[] = ( '' !== $row[ $header_key ] ) ? $row[ $header_key ] : '';
    }

    fputcsv( $this->stream, $data, $this->delimiter, $this->enclosure );
}

The error lines are 709:

$row[ $header_key ] = '';

and 713:

$data[] = ( '' !== $row[ $header_key ] ) ? $row[ $header_key ] : '';

Looking around at some similar questions, it looks like it is to do with isset: as in this answer and in this one. I'm not sure how to fix the code I have to make this work, or if that is indeed the actual problem and not something else.

The exported csv file contains all the warning messages each on a new line after each order.

Any help appreciated.

  • 写回答

1条回答 默认 最新

  • dragon88112 2015-02-17 05:19
    关注

    The function seems to be a bit of an overkill for me, so I'll rewrite it in a simpler to understand way.

    private function write( $row ) {
    
    $data = array();
    
    foreach ( $this->headers as $header_key => $header_value ) {
    
        if ( !isset($row[$header_key]) ){
            $row[$header_key] = '';
        }
        $data[]=$row[$header_key];
    }
    
    fputcsv( $this->stream, $data, $this->delimiter, $this->enclosure );
    }
    

    And I crunched it down because all the code really does is assumes theres a stream, delimiter, enclosure variables and headers array all stored elsewhere within the class. if the headers array doesn't consist of alphanumeric indexes then you'll get an error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog