Wordpress PHP 非法字符串错误

Wordpress PHP illegal string error

我在使用 wordpress 插件(Customer Order CSV Export)时遇到了一些问题,由于某种原因,该插件已开始将错误警告放入导出的文件中。 它说订单的每个部分都有一个非法的字符串偏移量(因此每个文件大约有 40 个警告)。我只附上了 order_number 的警告,但其余的都说了相同的行:

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

php 文件中的违规行如下所示:

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

错误行是 709:

$row[ $header_key ] = '';

和 713:

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

看了一些类似的问题,好像是和isset有关的: as in this answer and in this one。 我不确定如何修复我必须使它工作的代码,或者这是否确实是实际问题而不是其他问题。

导出的 csv 文件包含所有警告消息,每个消息在每个订单后各占一行。

感谢任何帮助。

这个函数对我来说似乎有点矫枉过正,所以我会用更容易理解的方式重写它。

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

我把它压缩了,因为所有代码真正做的是假设有一个流、定界符、外壳变量和 headers 数组都存储在 class 的其他地方。如果 headers 数组不包含字母数字索引,则会出现错误。