Php 如何使用Symfony对Csv进行编码

Php 如何使用Symfony对Csv进行编码,php,symfony,csv,serialization,components,Php,Symfony,Csv,Serialization,Components,我使用的是serializer组件的symfony,这里有一个将序列化和对象转换为JSON格式的示例,接下来: $encoders = array(new XmlEncoder(), new JsonEncoder()); $normalizers = array(new ObjectNormalizer()); $person = new \AppBundle\Entity\Person(); $person->setName('foo'); $

我使用的是serializer组件的symfony,这里有一个将序列化和对象转换为JSON格式的示例,接下来:

    $encoders = array(new XmlEncoder(), new JsonEncoder());
    $normalizers = array(new ObjectNormalizer());

    $person = new \AppBundle\Entity\Person();
    $person->setName('foo');
    $person->setAge(99);
    $person->setSportsman(false);

    // return new JsonResponse($person); // empty

    $serializer = new Serializer($normalizers, $encoders);

    /**
     * 1 param - object to be serialized
     * 2 param - proper encoder, in this case JsonEncoder
     * @var [type]
     */
    $jsonContent = $serializer->serialize($person, 'json');

    return new Response($jsonContent);
上面的示例返回如下内容:

{"id":null,"age":99,"name":"foo","sportsman":false}

但是我想知道如何做同样的事情,从Symfony 3.2开始,CSV和YAML编码器被添加到了
CSV
,而不是
JSON

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

您可以在那里找到更多信息:

自Symfony 3.2以来添加了CSV和YAML编码器

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

您可以在那里找到更多信息:

?????我尝试了相同的代码,但得到了一个空数组。第一个$serializer变量的用途是什么?我尝试了相同的代码,但得到了一个空数组。第一个$serializer变量的用途是什么?