2016-09-19 74 views
0

我不是!一個PHP開發人員,而這個任務仍然在我的桌子上結束。爲分銷商創建一些報告。 我已經完成了我的查詢並獲取了我的數據。但我有麻煩的數組轉換爲CSV格式的字符串,我可以爲客戶端服務。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致命錯誤:調用/usr/share/nginx/html/src/app/Rest/Controllers/DistributorReportController.php中的數組成員函數fetch()上線54

+0

$原料是一個數組,項目對象,所以使用的foreach和地址項使用循環數組 - >操作 –

回答

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

我已經upvoted大家:)

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()

2

首先,它看起來像你已經有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                                             
     );                                                 
    } 
2

您正在獲取對象數組,因此您無法在數組上使用提取方法。

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