CSV encoder¶

This encoder/decoder is ideal to import/export application data to programs like Excel:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 // instantiation, when using it as a component use Symfony\Component\Serializer\Serializer ; use Symfony\Component\Serializer\Encoder\CsvEncoder ; use Symfony\Component\Serializer\Normalizer\ObjectNormalizer ; $serializer = new Serializer ([ new ObjectNormalizer ()], [ new CsvEncoder ()]); // instantiation, when using it inside the Symfony framework $serializer = $container -> get ( 'serializer' ); // encoding contents in CSV format $serializer -> encode ( $data , 'csv' ); // decoding CSV contents $data = $serializer -> decode ( file_get_contents ( 'data.csv' ), 'csv' );

When decoding CSV contents, the first line must be the header with the names of the columns, which will be transformed into the object properties.

The CSV encoder/decoder also supports complex and nested data structures: