我正在尝试弄清楚如何获取具有以下格式的 JSON 数组
[
["2016-02-26","5190","1253","425","123"],
["2016-02-27","5209","1114","521","214"],
["2016-02-28","5142","1425","412","156"],
["2016-02-29","5523","1365","632","198"],
["2016-03-01","5125","1452","523","152"],
["2016-03-02","5000","1245","741","286"]
]
但目前我得到
[{"date":"2016-02-26","visitors":"5190","registered":"1253","downloaded":"425","sticky_activity":"123"},
{"date":"2016-02-27","visitors":"5209","registered":"1114","downloaded":"521","sticky_activity":"214"},
{"date":"2016-02-28","visitors":"5142","registered":"1425","downloaded":"412","sticky_activity":"156"},{"date":"2016-02-29","visitors":"5523","registered":"1365","downloaded":"632","sticky_activity":"198"},
{"date":"2016-03-01","visitors":"5125","registered":"1452","downloaded":"523","sticky_activity":"152"},
{"date":"2016-03-02","visitors":"5000","registered":"1245","downloaded":"741","sticky_activity":"286"}]
这是我的功能,如果它有帮助的话
public function dataAction(Request $request){
$em = $this->getDoctrine()->getEntityManager();
$connection = $em->getConnection();
$sqlQuery = "SELECT DATE_FORMAT(date, \"%Y-%m-%d\") as date,visitors, registered, downloaded, sticky_activity
FROM engagement";
$statement = $connection->prepare($sqlQuery);
$statement->execute();
$queryResult = $statement->fetchAll();
return new JsonResponse($queryResult);
}
最佳答案
返回这个:
return new JsonResponse(array_map('array_values', $queryResult));
顺便说一下,您还应该避免直接的 SQL 调用以支持 DQL或 query builders .这些是使用 Doctrine 时的良好实践。它的目标是尽可能避免 SQL。
关于php - Symfony2 MySql : Fetch array and get JSON array without column names,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35774062/