I have this code, below, where I manipulate some data. But after, I need to transform into a JSON object, like the example below.
<?php
function mostraContasEntrada($id_empresa){
$pdo = conectar();
$this->mostraDadosEntrada=$pdo->prepare(
"SELECT c.categoria, sc.subcategoria, data, valor
FROM entrada e
JOIN cat_entradas c
on c.id_categoria = e.categoria
JOIN sub_cat_entrada sc
on sc.id_subcategoria
WHERE id_empresa=:id_empresa
ORDER BY data DESC");
$this->mostraDadosEntrada->bindValue(":id_empresa", $id_empresa);
$this->mostraDadosEntrada->execute();
while ($r = $this->mostraDadosEntrada->fetch(PDO::FETCH_ASSOC)) {
$dataP = explode("-", $r['data']);
$data = $dataP[2].'/'.$dataP[1].'/'.$dataP[0];
echo $data.' '.$r['categoria'].' '.utf8_encode($r['subcategoria']).'
'.number_format($r['valor'],2,',','.')."<br>";
}
?>
I want to do this:
<?php
header('Content-Type: application/json');
$return = array();
while ($linha=$buscarUsuario->fetch(PDO::FETCH_ASSOC)) {
array_push($return, $linha);
}
echo json_encode($return);
?>