Kiksen
Kiksen

Reputation: 1777

PHP array to csv string

I am NOT! a php developer and still this task ended on my desk. Creating some report for distributor. I have made my query and getting my data out. But I am having troubles of converting the array into a CSV formatted string I can serve the clients.

The result I get:

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"
  }
}

I have tried lots of stuff like: Headers first (which worked)

  fputcsv($handle, array('id', 'master_user_id', 'company_name', 'price_per_register', 'price', 'kickback_percent', 'kickback', 'distributor_report_id'),',');

and then

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                                                                                                                                                                                
    );                                                                                                                                                                                                  
}                                                                                                                                                                                                       

Hope anyone can give me some nice pointer what I am doing wrong. I have seen this in the logs: NOTICE: PHP message: PHP Fatal error: Call to a member function fetch() on array in /usr/share/nginx/html/src/app/Rest/Controllers/DistributorReportController.php on line 54

Upvotes: 0

Views: 1381

Answers (4)

Kiksen
Kiksen

Reputation: 1777

Hi Thx all for the awesome feedback. You all lead me in the right direction. My result:

$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);

I have upvoted all of you :)

Upvotes: 0

Tadas Paplauskas
Tadas Paplauskas

Reputation: 1903

You are getting an array of objects, so you cannot use fetch method on an array.

$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), ',');                                                                                                                                                                                             
}             

Upvotes: 2

Gvidas
Gvidas

Reputation: 1964

First it looks like you already have array so you don't need to fetch(), second you try access object property ($row->id) as array element ($row['id']). Here's example that should work:

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                                                                                                                                                                                
        );                                                                                                                                                                                                  
    }  

Upvotes: 3

First you need to extract the objects from the array, then iterate through the objects to populate the 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 file is now populated, so use fclose()

Upvotes: 1

Related Questions