1

I am trying to figure out how to get JSON array with following format

  [
     ["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"]
   ]

But currently I am getting

  [{"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"}]

Here is my function, if it's gonna help

  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);
}
3
  • Use array_values() Commented Mar 3, 2016 at 14:08
  • i guess GROUP_CONCAT() is what you are looking for, unfortunatly i cant go any further Commented Mar 3, 2016 at 14:33
  • @malcolm no, it didn't help. I am still getting column names in array Commented Mar 3, 2016 at 14:33

1 Answer 1

1

Return this:

return new JsonResponse(array_map('array_values', $queryResult));

By the way you should also avoid direct SQL calls in favor of DQL or query builders. These are good practices while using Doctrine. Its goal is to avoid SQL as much as possible.

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

1 Comment

Yeah, I would use DQL, but 'engagement' is not a class, it's just a table

Your Answer

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