我不是!一个PHP开发人员,仍然这个任务在我的桌面上结束。为经销商创建一些报告。 我已经查询并获取了数据。但我遇到了将阵列转换为CSV格式字符串的麻烦,我可以为客户提供服务。
我得到的结果:
array(1) {
[0]=>
object(stdClass)#351 (9) {
["id"]=>
string(36) "cc23b3b9-7e38-11e6-b6fa-0a1bcd0d7087"
["master_user_id"]=>
string(36) "84d55a15-5256-2ee6-1d31-8f3ccbc6f223"
["company_name"]=>
string(7) "Tellest"
["price_per_register"]=>
string(1) "0"
["num_of_registers"]=>
string(1) "1"
["price"]=>
string(5) "18625"
["kickback_percent"]=>
string(4) "0.25"
["kickback"]=>
string(4) "4656"
["distributor_report_id"]=>
string(3) "260"
}
}
我尝试过很多东西: 首先是标题(有效)
fputcsv($handle, array('id', 'master_user_id', 'company_name', 'price_per_register', 'price', 'kickback_percent', 'kickback', 'distributor_report_id'),',');
然后
while($row = $results->fetch()) {
fputcsv(
$handle, // The file pointer
array($row['id'], $row['master_user_id'], $row['company_name'], $row['price_per_register'], $row['price'], $row['kickback_percent'], $row['kickback'], $row['distributor_report_id']), // The fields
',' // The delimiter
);
}
希望任何人都可以给我一些很好的指针我做错了什么。 我在日志中看到过这个: 注意:PHP消息:PHP致命错误:在第54行的/usr/share/nginx/html/src/app/Rest/Controllers/DistributorReportController.php中调用数组上的成员函数fetch()
答案 0 :(得分:3)
首先看起来你已经有array
所以你不需要fetch()
,第二次尝试访问对象属性($row->id
)作为数组元素($row['id']
)。这是应该有效的例子:
foreach($results as $row){
fputcsv(
$handle, // The file pointer
array($row->id, $row->master_user_id, $row->company_name, $row->price_per_register, $row->price, $row->kickback_percent, $row->kickback, $row->distributor_report_id), // The fields
',' // The delimiter
);
}
答案 1 :(得分:2)
您正在获取一个对象数组,因此您无法在数组上使用fetch方法。
$rows = $results->fetch(); // or whatever else you do to get the presented array :)
foreach ($rows as $row) {
fputcsv($handle,
$row->id, $row->master_user_id, $row->company_name, $row->price_per_register, $row->price, $row->kickback_percent, $row->kickback, $row->distributor_report_id), ',');
}
答案 2 :(得分:1)
首先,您需要从数组中提取对象,然后遍历对象以填充csv:
$array = //a collection of what you show at the top off post - I'm assuming more than 1 member
$count = count($array);
for($i = 0; $i < $count; $i++) {
$object = $array[$i];// iterate through the array of objects
foreach($object as $obj) {
// in here populate the csv with this object's members, use object notation with ->
$fields = array($obj->id, $obj->master_user_id, $obj->company_name, $obj->price_per_register, $obj->price, $obj->kickback_percent, $obj->kickback, $obj->distributor_report_id);
fputcsv($handle, $fields, ',');// comma is the default separator
}//end foreach
}//end for loop
现在已填充csv文件,因此请使用fclose()
答案 3 :(得分:0)
嗨Thx所有人都提供了很棒的反馈意见。 你们都引导我朝着正确的方向前进。 我的结果:
$handle = fopen('php://output', 'w+');
fputcsv($handle, array('id', 'master_user_id', 'company_name', 'price_per_register', 'num_of_registers' , 'price', 'kickback_percent', 'kickback', 'distributor_report_id'),',');
$count = count($results);
for($i = 0; $i < $count; $i++) {
$object = $results[$i];// iterate through the array of objects
echo implode(",",$object);
echo "\n";
}
fclose($handle);
我赞成你们所有人:)