我想问一下如何将php数组std对象写入csv文件。
Convert array to csv没有解决我的问题。
这是我的数组
Array
(
[0] => stdClass Object
(
[lotNum] => 1
[commissionRate] => 4.0%
[detailUrl] => JEq7E6IEa
[promotionVolume] => 0
[packageType] => piece
[price] => US $80.54
[imageUrl] => BOX.jpg
[subject] => Box
[salePrice] => US $60.41
[commission] => US $0.00
[productId] => xxxx1
)
[1] => stdClass Object
(
[lotNum] => 1
[commissionRate] => 4.0%
[detailUrl] => JEq7E6IEa
[promotionVolume] => 0
[packageType] => piece
[price] => US $80.54
[imageUrl] => BOX.jpg
[subject] => Box
[salePrice] => US $60.41
[commission] => US $0.00
[productId] => xxxx1
)
)
如何将每个产品的数据放在一行csv文件中? 谢谢:))
答案 0 :(得分:8)
尝试以下
<?php
//$list = ----your array
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp,get_object_vars($fields));
}
fclose($fp);
?>
答案 1 :(得分:2)
您的阵列中有对象。将它们转换为数组。
<?php
$fp = fopen('file.csv', 'w');
foreach ($myArray as $fields) {
if( is_object($fields) )
$fields = (array) $fields;
fputcsv($fp, $fields);
}
fclose($fp);
?>