Why does the JSON output show both the index number and the association colume name?

I only need just column name such as "UID"

PHP CODE

$res = $statement->fetchAll();
$records = array('Record'=>$posts);
echo json_encode($records);

JSON OUTPUT

{
  "Record":[
    {
      "UID":"1001",
      "0":"1001",
      "NAME":"Robot2",
      "1":"Robot2",
      "EMAIL":"[email protected]",
      "2":"[email protected]",
      "GENDER":"f",
      "3":"f"
    },
    {
      "UID":"1030",
      "0":"1030",
      "NAME":"Anna",
      "1":"Anna",
      "EMAIL":"[email protected]",
      "2":"[email protected]",
      "GENDER":"f",
      "3":"f"
    }
  ]
}

Thanks

share|improve this question
That's how mysql query responses are. – Austin Brunkhorst Oct 22 '12 at 15:06

2 Answers

up vote 1 down vote accepted

See the documentation for an explaination:

http://php.net/manual/en/pdostatement.fetchall.php

In order to only get the associative items, you should pass PDO::FETCH_ASSOC to your fetchAll.

share|improve this answer
Thanks for correcting me and giving accurate answer to my question. – mohsin.mr Oct 22 '12 at 17:24

The default fetch style is PDO::FETCH_BOTH, you need to set it to PDO::FETCH_ASSOC.

$res = $statement->fetchAll(PDO::FETCH_ASSOC);

Or you could set the default DEFAULT_FETCH_MODE by:

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

then you could just do $res = $statement->fetchAll();

share|improve this answer
you guys are fast on the trigger... :-) – prodigitalson Oct 22 '12 at 15:05

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.