$mugi = $repo->find(1); $sora = $repo->find(2); $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader())); $metadataAwareNameConverter = new MetadataAwareNameConverter($classMetadataFactory); $normalizers = [new ObjectNormalizer($classMetadataFactory, $metadataAwareNameConverter)]; $encoders = [new CsvEncoder(), new JsonEncode()]; $serializer = new Serializer($normalizers, $encoders); $data = [$mugi, $sora]; $csv = $serializer->serialize($mugi, 'csv'); $json = $serializer->serialize($mugi, 'json'); echo $csv; echo "---\n"; echo $json; // output // id,name,image_url,gender,weight // 1,Mugi,mugi.jpg,Female,1.8 // --- // {"id":1,"name":"Mugi","image_url":"mugi.jpg","gender":"Female","weight":1.8} app/300/390_serialize_csv_json.php