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 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题