将数组转换为 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
(
[Product] => stdClass Object
(
[productId] => 674975
[productCode] =>
[productDescription] =>
[units] => 10
[unitPrice] => 39.95
[unitTaxRate] => 0
[totalProductPrice] => 399.5
[productName] => Acne Clearing Gel
)
)
[addresses] => stdClass Object
(
[Address] => stdClass Object
(
[addressTypeID] => 8
[addressLine1] => Cebu City
[city] => Cebu
[zipcode] => 6000
[state] =>
[countryCode] => PH
)
)
)
)
)
How to convert an array into a CSV file?
This is my array:
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
(
[Product] => stdClass Object
(
[productId] => 674975
[productCode] =>
[productDescription] =>
[units] => 10
[unitPrice] => 39.95
[unitTaxRate] => 0
[totalProductPrice] => 399.5
[productName] => Acne Clearing Gel
)
)
[addresses] => stdClass Object
(
[Address] => stdClass Object
(
[addressTypeID] => 8
[addressLine1] => Cebu City
[city] => Cebu
[zipcode] => 6000
[state] =>
[countryCode] => PH
)
)
)
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我为此使用以下函数;它改编自 fputscsv 评论中的 man 条目之一。您可能想要展平该数组;不确定如果你传入一个多维的会发生什么。
I'm using the following function for that; it's an adaptation from one of the man entries in the fputscsv comments. And you'll probably want to flatten that array; not sure what happens if you pass in a multi-dimensional one.
我的解决方案要求数组的格式与问题中提供的格式不同:
我们定义我们的函数:
然后我们将数据输出为 CSV:
我已经在几个项目中使用了它,并且效果很好。我应该注意到
outputCSV
代码比我更聪明,所以我确信我不是原作者。不幸的是,我已经不知道从哪里得到它了,所以我不能把它归功于谁。My solution requires the array be formatted differently than provided in the question:
We define our function:
Then we output our data as a CSV:
I have used this with several projects, and it works well. I should note that the
outputCSV
code is more clever than I am, so I am sure I am not the original author. Unfortunately I have lost track of where I got it, so I can't give the credit to whom it is due.当您想要在模板中创建和回显 CSV 时,kingjeffrey 对上述解决方案进行了轻微的调整(即,大多数框架将启用输出缓冲,并且您需要在控制器中设置标头等。)
感谢上面的 Kingjeffrey 以及到这篇博客文章,我在其中找到了有关创建文本流的信息。
A slight adaptation to the solution above by kingjeffrey for when you want to create and echo the CSV within a template (Ie - most frameworks will have output buffering enabled and you are required to set headers etc in controllers.)
Credit to Kingjeffrey above and also to this blog post where I found the information about creating text streams.
根据接受的答案添加一些改进。
Add some improvements based on accepted answer.
保罗接受的答案很棒。我对此做了一个小扩展,如果你有一个像这样的多维数组(这很常见),这非常有用:
所以我只是从上面获取了 Paul 的函数:
并添加了这个:
所以你可以调用:
如果你想要特殊的行结尾,您可以为此使用可选参数“glue”。
The accepted answer from Paul is great. I've made a small extension to this which is very useful if you have an multidimensional array like this (which is quite common):
So I just took Paul's function from above:
And added this:
So you can just call:
If you want a special line ending you can use the optional parameter "glue" for this.
好吧,也许 4 年后有点晚了哈哈...但我一直在寻找将 OBJECT 转换为 CSV 的解决方案,但是这里的大多数解决方案实际上是针对 ARRAY 到 CSV 的...
经过一些修补,这是我将对象转换为 CSV 的解决方案,我认为非常整洁。希望这会帮助别人。
请注意,对于
$key => $value
要工作,您的对象
的属性必须是公共的,私有属性将不会被获取。最终结果是你得到这样的结果:
Well maybe a little late after 4 years haha... but I was looking for solution to do OBJECT to CSV, however most solutions here is actually for ARRAY to CSV...
After some tinkering, here is my solution to convert object into CSV, I think is pretty neat. Hope this would help someone else.
Note that for the
$key => $value
to work, yourobject
's attributes must be public, the private ones will not get fetched.The end result is that you get something like this: