PHP - Export to CSV an array of objects

I want to export an array of objects to 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"
}
}

In something like:

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

First line: list of attributes / columns

Other line: object attribute value

True Example of an array with an object: http://pastebin.com/8Eyf46pb

I tried this: Convert an array to csv , but it does not work for me.

Is it possible to do this (and how?), Or should I write each attribute in a loop?

+4
source share
2 answers

It would be pretty easy if all of your properties were publicly available:

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

, , . , , (). :

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

typecasting toArray :

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

getProperties :

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

, , . .

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

Source: https://habr.com/ru/post/1526213/


All Articles