I have the following webservice in php
<?php
if(isset($_GET['user']))
$idActeur = ($_GET['user']);
$pdo = PdoGsb::getPdoGsb();
$tabVisites = $pdo->getLesVisites($idActeur);
echo json_encode(array('visite'=>$tabVisites));
?>
this webservice returns json of this format
[{"id":"5","0":"5","dateVisite":"2015-12-31","1":"2015-12-31","estVisiteProgramme":"1","2":"1","heureArriveeCabinet":"23h58","3":"23h58","heureDebutEntretien":"23h59","4":"23h59","heureDepartCabinet":"23h59","5":"23h59","idMedecin":"3","6":"3","idActeur":"a17","7":"a17"},
When doing a var_dump of the variable that is outputed in json I get this
array (size=6)
0 =>
array (size=16)
'id' => string '5' (length=1)
0 => string '5' (length=1)
'dateVisite' => string '2015-12-31' (length=10)
1 => string '2015-12-31' (length=10)
'estVisiteProgramme' => string '1' (length=1)
2 => string '1' (length=1)
'heureArriveeCabinet' => string '23h58' (length=5)
3 => string '23h58' (length=5)
'heureDebutEntretien' => string '23h59' (length=5)
4 => string '23h59' (length=5)
'heureDepartCabinet' => string '23h59' (length=5)
5 => string '23h59' (length=5)
'idMedecin' => string '3' (length=1)
6 => string '3' (length=1)
'idActeur' => string 'a17' (length=3)
7 => string 'a17' (length=3)
how should I rework my webservice so that the outputed json does not include the array indexes ?
the goal is to have a json that looks like this:
[{"id":"5",
"dateVisite":"2015-12-31",
"estVisiteProgramme":"1 }]
Here is the method that fetch the results from the database:
public function getLesVisites($idActeur)
{
// Création requête
$req = "SELECT * FROM visite WHERE visite.idActeur = '$idActeur'";
// Exécution de la requête
$res = PdoGsb::$monPdo->query($req);
// Stockage de la requête dans la variable $lesVisites
$lesVisites = $res->fetchAll();
// Retourne les visites
return $lesVisites;
}
PDO::FETCH_MODEtoPDO::FETCH_ASSOC. The default isPDO::FETCH_BOTH, which is what you're getting.$pdo->setAttrValue()to change the fetch mode right after making the database connection (I assume in yourPdoGsbclass), or you can supply a fetch mode as an argument whenever you callfetch()orfetchAll()on a PDO result.$result->fetchAll(), call$result->fetchAll(PDO::FETCH_ASSOC). See the manual for more info