Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

php - Split rows in csv representation using api-platform

I've an entity that stores a property inside an array. Here an example.

{
    "id": 42,
    "superheroes": {
        "hero": {
            "name": "Batman"
        },
        "antihero": {
            "name": "Joker"
        }
    },
    "id": 43,
    "superheroes": {
        "hero": {
            "name": "Bing"
        },
        "antihero": {
            "name": "Flop"
        }
    },
}

When I tried to represent this kind of information using api-platform, I got this:

id, superhero.hero.name, superhero.antihero.name
42, Batman, Joker

What I need instead is this kind of output. I mean, ... Each line must be exploded into more lines.

id, name
42, Batman
42, Joker
43, Bing
43, Flopp

I am trying to solve the issue creating a normalizer. But this attempt give me strange ouput:

    /**
     * @param AppEntityInsurance $object
     * @param string|null $format
     * @param array $context
     * @return array|ArrayObject|bool|float|int|string|null
     */
    public function normalize($object, string $format = null, array $context = [])
    {
        return [
            [
                $object->getId(),
                $object->getSuperheroes()['hero']['name'],
            ], [
                $object->getId(),
                $object->getSuperheroes()['antihero']['name'],
            ],
        ];
    }
   0.0,0.1,1.0,1.1
    42,Batman,42,Batman
    43,Bing,43,Bing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...