PHP mysql结果json_encode返回键和索引号

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":"robot2@test.com",
      "2":"robot2@test.com",
      "GENDER":"f",
      "3":"f"
    },
    {
      "UID":"1030",
      "0":"1030",
      "NAME":"Anna",
      "1":"Anna",
      "EMAIL":"Anna@msn.com",
      "2":"Anna@msn.com",
      "GENDER":"f",
      "3":"f"
    }
  ]
}

Thanks

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.

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();