2014-02-10 77 views
4

數組我想導出CSV對象的數組:PHP - 導出爲CSV對象

array(10) { 
    [0]=> 
    object(Produit)#228 (36) { 
    ["id_produit":protected]=> string(4) "9999" 
    ["reference":protected]=> string(9) "reference1" 
} 
    [1]=> 
    object(Produit)#228 (36) { 
    ["id_produit":protected]=> string(4) "8888" 
    ["reference":protected]=> string(9) "reference2" 
} 
} 

的東西,如:

id_produit | reference | ... 
9999 | reference1 | ... 
8888 | reference2 | ... 

第一行:attribut的列表/柱

另一行:對象的attribut的

數組與對象

True示例:http://pastebin.com/8Eyf46pb

我嘗試這樣做:Convert array into csv但它不爲我工作。

是否有可能做到這一點(以及如何?)或我必須在循環中寫入每個屬性?

回答

6

這將是非常容易的,如果你所有的性質是公開的:

// Test class 
class Produit 
{ 
    public $id_produit; 
    public $reference; 

    // Test data 
    public function __construct() 
    { 
     $this->id_produit = rand(1, 255); 
     $this->reference = rand(1, 255); 
    } 
} 

// Test data array 
$array = array(new Produit(), new Produit()); 

// Notice, you can only use a single character as a delimiter 
$delimiter = '|'; 

if (count($array) > 0) { 
    // prepare the file 
    $fp = fopen('test/file.csv', 'w'); 

    // Save header 
    $header = array_keys((array)$array[0]); 
    fputcsv($fp, $header, $delimiter); 

    // Save data 
    foreach ($array as $element) { 
     fputcsv($fp, (array)$element, $delimiter); 
    } 
} 

但是,正如我所看到的,您的屬性受到保護。這意味着我們無法訪問對象之外的屬性以及循環遍歷它們,或者使用類型轉換來使用(array)。因此,在這種情況下,你必須做出一些改變加時賽對象:

// Test class 
class Produit 
{ 
    // ... 

    public function getProperties() 
    { 
     return array('id_produit', 'reference'); 
    } 

    public function toArray() 
    { 
     $result = array(); 

     foreach ($this->getProperties() as $property) { 
      $result[$property] = $this->$property; 
     } 

     return $result; 
    } 
} 

然後,而不是類型轉換,你可以使用新的方法指定者這樣的:

// Save data 
foreach ($array as $element) { 
    fputcsv($fp, $element->toArray(), $delimiter); 
} 

也感謝新mehod的GetProperties,我們可以改變頭部得到:

// Save header 
fputcsv($fp, $array[0]->getProperties(), $delimiter); 
+0

謝謝你,我測試了你的每個例子,並且我得到了相同的結果,這是否正常? (即使我的房產受到保護) – Snow

1

我現在測試了下面的代碼,它似乎工作。反思就是答案。

我試圖得到它的工作在phpfiddle因此PHP://溫度,但它沒有工作,不幸

<?php 
class Produit 
{ 
    public $id_produit; 
    public $reference; 

    // Test data 
    public function __construct() 
    { 
     $this->id_produit = rand(1, 255); 
     $this->reference = rand(1, 255); 
    } 
} 

$array = array(new Produit(), new Produit()); 


$delimiter='|'; 
$fp=fopen('php://temp', 'w'); //replace this bit with a file name 
$header=false; 
foreach($array as $Produit){ 
    $Reflection = new ReflectionClass($Produit); 
    $properties = $Reflection->getProperties(); 
    $row=array(); 
    foreach($properties as $prop){ 
     $row[$prop->getName()] = $prop->getValue($Produit); 
    } 
    if(!$header){ 
     fputcsv($fp, array_keys($row), $delimiter); 
     $header=true; 
    } 
    fputcsv($fp, $row, $delimiter); 
} 

//now show what has been written, you will want to remove this section 
fseek($fp, 0); 
fpassthru($fp); 
//ends 

fclose($fp); 
+0

謝謝,我有同樣的結果比上面的其他職位。 – Snow