【问题标题】:PHP - Export to CSV an array of objectsPHP - 将对象数组导出为 CSV
【发布时间】:2014-03-08 03:45:29
【问题描述】:

我想以 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 | ...

第一行:属性/列列表

另一行:Object的属性值

带有对象的数组的真实示例: http://pastebin.com/8Eyf46pb

我试过这个:Convert array into csv 但它对我不起作用。

是否可以这样做(以及如何?)或者我必须在循环中编写每个属性?

【问题讨论】:

    标签: php arrays object csv export


    【解决方案1】:

    如果您的所有属性都是公开的,那就很容易了:

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

    然后你可以像这样使用新方法 toArray 来代替类型转换:

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

    也感谢新方法 getProperties,我们可以更改标题获取:

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

    【讨论】:

    • 谢谢,我测试了你的每一个例子,结果都是一样的,正常吗? (即使我的财产受到保护)
    【解决方案2】:

    我现在已经测试了下面的代码,它似乎确实有效。反思就是答案。

    我试图让它在 phpfiddle 上工作,因此 php://temp 但不幸的是它没有工作

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

    【讨论】:

    • 谢谢,我得到的结果和上面的其他帖子一样。
    猜你喜欢
    • 2018-03-06
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-05
    • 2016-03-21
    相关资源
    最近更新 更多