0
I am trying to get the following return in Mysql:
{
    matricula : '123',
        periodo : [
            {id : '012016', titulo: 'Janeiro 2016'},
            {id : '022016', titulo: 'Fevereiro 2016'},
            {id : '032016', titulo: 'Março 2016'}
        ]
    },
    matricula : '456',
        periodo : [
            {id : '012017', titulo: 'Janeiro 2017'},
            {id : '022017', titulo: 'Fevereiro 2017'},
            {id : '032017', titulo: 'Março 2017'}
        ]
    },
}
I’m using the Slim Rest with this Code:
$sql = "
    SELECT
        nu_matricula AS matricula,
        CONCAT(nu_mes, nu_ano) AS periodo,
        (CASE nu_mes 
         when 1 then CONCAT('Janeiro','/',nu_ano)
         when 2 then CONCAT('Fevereiro','/',nu_ano)
         when 3 then CONCAT('Março','/',nu_ano)
         when 4 then CONCAT('Abril','/',nu_ano)
         when 5 then CONCAT('Maio','/',nu_ano)
         when 6 then CONCAT('Junho','/',nu_ano)
         when 7 then CONCAT('Julho','/',nu_ano)
         when 8 then CONCAT('Agosto','/',nu_ano)
         when 9 then CONCAT('Setembro','/',nu_ano)
         when 10 then CONCAT('Outubro','/',nu_ano)
         when 11 then CONCAT('Novembro','/',nu_ano)
         when 12 then CONCAT('Dezembro','/',nu_ano)
      END) AS titulo
    FROM contra_cheque
    ORDER BY id_contra_cheque DESC
";
try {
    $db     = getDB();
    $stmt   = $db->query($sql);
    $users  = $stmt->fetchAll(PDO::FETCH_GROUP);
    $db     = NULL;
    echo '{"dados": ' . json_encode($users) . '}';
} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}
And returns this result:
{
    "contra_cheques": {
        "1731793": [{
            "periodo":"012018",
            "0":"012018",
            "titulo":"Janeiro\/2018",
            "1":"Janeiro\/2018"
        },{
            periodo":"122017",
            "0":"122017",
            "titulo":"Dezembro\/2017",
            "1":"Dezembro\/2017"
        },{
"periodo":"112017","0":"112017","titulo":"Novembro\/2017","1":"Novembro\/2017"},{"periodo":"102017","0":"102017","titulo":"Outubro\/2017","1":"Outubro\/2017"},{"periodo":"092017","0":"092017","titulo":"Setembro\/2017","1":"Setembro\/2017"},{"periodo":"082017","0":"082017","titulo":"Agosto\/2017","1":"Agosto\/2017"},{"periodo":"072017","0":"072017","titulo":"Julho\/2017","1":"Julho\/2017"},{"periodo":"062017","0":"062017","titulo":"Junho\/2017","1":"Junho\/2017"}]}}
How to get the JSON result above desired??