doujichan1399
2015-02-17 05:03 阅读 48
已采纳

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 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.

    点赞 评论 复制链接分享

相关推荐