将数组转换为csv

如何将数组转换为CSV文件?


这是我的数组:


stdClass Object

(


    [OrderList_RetrieveByContactResult] => stdClass Object

        (

            [OrderDetails] => stdClass Object

                (

                    [entityId] => 1025298

                    [orderId] => 10952

                    [orderName] => testing

                    [statusTypeId] => 4652

                    [countryCode] => AU

                    [orderType] => 1

                    [invoiceNumber] => 0

                    [invoiceDate] => 0001-01-01T00:00:00

                    [userID_AssignedTo] => 11711

                    [shippingAmount] => 8.95

                    [shippingTaxRate] => 0

                    [shippingAttention] => 

                    [shippingInstructions] => 

                    [shippingOptionId] => 50161

                    [discountCodeId] => 0

                    [discountRate] => 0

                    [totalOrderAmount] => 408.45

                    [directDebitTypeId] => 0

                    [directDebitDays] => 0

                    [isRecur] => 

                    [nextInvoiceDate] => 0001-01-01T00:00:00

                    [endRecurDate] => 0001-01-01T00:00:00

                    [cycleTypeID] => 1

                    [createDate] => 2010-10-08T18:40:00

                    [lastUpdateDate] => 2010-10-08T18:40:00

                    [deleted] => 

                    [products] => stdClass Object

                      )


                )


        )


)


跃然一笑
浏览 648回答 3
3回答

子衿沉夜

我的解决方案要求数组的格式与问题中提供的格式不同:<?&nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; array( 'row_1_col_1', 'row_1_col_2', 'row_1_col_3' ),&nbsp; &nbsp; &nbsp; &nbsp; array( 'row_2_col_1', 'row_2_col_2', 'row_2_col_3' ),&nbsp; &nbsp; &nbsp; &nbsp; array( 'row_3_col_1', 'row_3_col_2', 'row_3_col_3' ),&nbsp; &nbsp; );?>我们定义我们的功能:<?&nbsp; &nbsp; function outputCSV($data) {&nbsp; &nbsp; &nbsp; &nbsp; $outputBuffer = fopen("php://output", 'w');&nbsp; &nbsp; &nbsp; &nbsp; foreach($data as $val) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fputcsv($outputBuffer, $val);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fclose($outputBuffer);&nbsp; &nbsp; }?>然后,我们将数据输出为CSV:<?&nbsp; &nbsp; $filename = "example";&nbsp; &nbsp; header("Content-type: text/csv");&nbsp; &nbsp; header("Content-Disposition: attachment; filename={$filename}.csv");&nbsp; &nbsp; header("Pragma: no-cache");&nbsp; &nbsp; header("Expires: 0");&nbsp; &nbsp; outputCSV($data);?>我已经在几个项目中使用了它,并且效果很好。我应该注意,该outputCSV代码比我更聪明,所以我确定我不是原始作者。不幸的是,我没有知道我从哪里得到的,所以我不能把它归功于谁。

胡说叔叔

当您要在模板中创建和回显CSV时,对kingjeffrey的上述解决方案稍作修改(即-大多数框架将启用输出缓冲,并且您需要在控制器中设置标头等)。// Create Some data<?php    $data = array(        array( 'row_1_col_1', 'row_1_col_2', 'row_1_col_3' ),        array( 'row_2_col_1', 'row_2_col_2', 'row_2_col_3' ),        array( 'row_3_col_1', 'row_3_col_2', 'row_3_col_3' ),    );// Create a stream opening it with read / write mode$stream = fopen('data://text/plain,' . "", 'w+');// Iterate over the data, writting each line to the text streamforeach ($data as $val) {    fputcsv($stream, $val);}// Rewind the streamrewind($stream);// You can now echo it's contentecho stream_get_contents($stream);// Close the stream fclose($stream);
打开App,查看更多内容
随时随地看视频慕课网APP