1

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;
    }
8
  • 4
    Set the PDO::FETCH_MODE to PDO::FETCH_ASSOC. The default is PDO::FETCH_BOTH, which is what you're getting. Commented May 6, 2017 at 17:51
  • Where should I do that ? Commented May 6, 2017 at 17:57
  • You can call $pdo->setAttrValue() to change the fetch mode right after making the database connection (I assume in your PdoGsb class), or you can supply a fetch mode as an argument whenever you call fetch() or fetchAll() on a PDO result. Commented May 6, 2017 at 18:00
  • when I switched to fetchAll to fetch_assoc it threw me an error was it what you recommended ? Commented May 6, 2017 at 18:06
  • 2
    No, don't change the function, add an argument. So instead of calling $result->fetchAll(), call $result->fetchAll(PDO::FETCH_ASSOC). See the manual for more info Commented May 6, 2017 at 18:10

1 Answer 1

2

Set the PDO::FETCH_MODE to PDO::FETCH_ASSOC. The default is PDO::FETCH_BOTH, which is what you're getting. You can call $pdo->setAttrValue() to change the fetch mode right after making the database connection (I assume in your PdoGsb class), or you can supply a fetch mode as an argument whenever you call fetch() or fetchAll() on a PDO result, e.g. $result->fetchAll(PDO::FETCH_ASSOC).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.