PHP数组到csv字符串

PHP数组到csv字符串,php,csv,Php,Csv,我不是!作为一名php开发人员,这项任务仍然在我的办公桌上结束。为分销商创建一些报告。 我已经提出了我的查询,并获得了我的数据。但是我很难将数组转换成CSV格式的字符串,我可以为客户端提供服务 我得到的结果是: array(1) { [0]=> object(stdClass)#351 (9) { ["id"]=> string(36) "cc23b3b9-7e38-11e6-b6fa-0a1bcd0d7087" ["master_user_id"]=

我不是!作为一名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"
  }
}
我试过很多东西,比如: 首先是标题(哪个有效)

然后

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(),首先需要从数组中提取对象,然后遍历对象以填充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
$array=//您在顶部文章中显示的内容的集合-我假设不止一个成员
$count=计数($array);
对于($i=0;$i<$count;$i++){
$object=$array[$i];//遍历对象数组
foreach($object作为$obj){
//在这里,使用此对象的成员填充csv,使用带->
$fields=array($obj->id,$obj->master\u user\u id,$obj->company\u name,$obj->price\u per\u register,$obj->price,$obj->kickback\u percent,$obj->kickback,$obj->distributor\u report\u id);
fputcsv($handle,$fields,,');//逗号是默认的分隔符
}//端部foreach
}//循环结束

csv文件现在已填充,因此使用

首先需要从数组中提取对象,然后迭代对象以填充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
$array=//您在顶部文章中显示的内容的集合-我假设不止一个成员
$count=计数($array);
对于($i=0;$i<$count;$i++){
$object=$array[$i];//遍历对象数组
foreach($object作为$obj){
//在这里,使用此对象的成员填充csv,使用带->
$fields=array($obj->id,$obj->master\u user\u id,$obj->company\u name,$obj->price\u per\u register,$obj->price,$obj->kickback\u percent,$obj->kickback,$obj->distributor\u report\u id);
fputcsv($handle,$fields,,');//逗号是默认的分隔符
}//端部foreach
}//循环结束

csv文件现在已填充,所以首先使用它看起来您已经有了
数组
,所以您不需要
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                                                                                                                                                                                
        );                                                                                                                                                                                                  
    }  

首先,看起来您已经有了
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                                                                                                                                                                                
        );                                                                                                                                                                                                  
    }  

您正在获取一个对象数组,因此无法对数组使用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), ',');                                                                                                                                                                                             
}             

您正在获取一个对象数组,因此无法对数组使用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), ',');                                                                                                                                                                                             
}             

嗨,谢谢你的反馈。 你们都把我引向正确的方向。 我的结果是:

$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);
$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=计数($results);
对于($i=0;$i<$count;$i++){
$object=$results[$i];//遍历对象数组
回波内爆(“,”,$object);
回音“\n”;
}                                                                                                                                                                             
fclose($handle);

我已经向你们所有人投了赞成票:)

大家好,谢谢你们的精彩反馈。 你们都把我引向正确的方向。 我的结果是:

$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);
$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=计数($results);
对于($i=0;$i<$count;$i++){
$object=$results[$i];//遍历对象数组
回波内爆(“,”,$object);
回音“\n”;
}