How to generate a json with parent-child format

Asked

Viewed 230 times

2

I need to do the following json by php

    {
  "status": "200",
  "data": [
    {
      "id": "1",
      "premio": "Fusca 74",
      "modelo": "Fusca 74",
      "descricao": "Fusca ano 1974 lindo\r\nFusca de colecionador Raríssimo  ",
      "marca": "VolksWagen",
      "pontos": "5000",
      "foto": "sistema/uploads/premios/1120735490945317.jpg",
      "pontos_usuario": "30000"
    },
    {
      "id": "2",
      "premio": "Carro VW",
      "modelo": "Modelo",
      "descricao": "Lindo carro VW",
      "marca": "VW",
      "pontos": "100000",
      "foto": "sistema/uploads/premios/17937693505959.jpg",
      "pontos_usuario": "30000"
    }
  ]
}

I did it that way but it didn’t work:

if($count > 0){
                $premios = array();
                //$curso_eventos["data"] = array();
                //$curso_eventos["count"] = $count;
                array_push($premios,array("status" => "200"));


                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){

                    extract($row);

                    $p  = array(
                    "id" => $id,
                    "premio" => $premio,
                    "modelo" => $modelo,
                    "descricao" => $descricao,
                    "marca" => $marca,
                    "pontos" => $pontos,
                    "foto" => $foto,
                    "pontos_usuario" => $pontos_usuario

                    );


                    array_push($premios, $p);
                    //array_push($curso_eventos["data"], $p);
                }

                echo json_encode($premios);
            }

The following json is generated:

[
    {
        "status": "200"
    },
    {
        "id": "1",
        "premio": "Fusca 74",
        "modelo": "Fusca 74",
        "descricao": "Fusca ano 1974 lindo\r\nFusca de colecionador Raríssimo  ",
        "marca": "VolksWagen",
        "pontos": "5000",
        "foto": "sistema/uploads/premios/1120735490945317.jpg",
        "pontos_usuario": "30000"
    },
    {
        "id": "2",
        "premio": "Carro VW",
        "modelo": "Modelo",
        "descricao": "Lindo carro VW",
        "marca": "VW",
        "pontos": "100000",
        "foto": "sistema/uploads/premios/17937693505959.jpg",
        "pontos_usuario": "30000"
    }
]

Where I’m wrong?

1 answer

2


Just attach "data" as a $premios key. You can also use the following syntax to create and add new items in the array:

if ($count > 0) {

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        extract($row);
        $data[] = [
            "id" => $id,
            "premio" => $premio,
            "modelo" => $modelo,
            "descricao" => $descricao,
            "marca" => $marca,
            "pontos" => $pontos,
            "foto" => $foto,
            "pontos_usuario" => $pontos_usuario
        ];
    }

    $premios = [
        "status" => "200"
        "data" => $data;
    ];
    echo json_encode($premios);
}
  • Perfect! Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.