2

In my database I have an entity named Graphique. This the schema for:

class Graphique
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var decimal
     *
     * @ORM\Column(name="index", type="decimal", precision=9, scale=3, nullable=false)
     */
    private $index;

    /**
     * @var datetime
     *
     * @ORM\Column(name="date", type="datetime", nullable=false)
     */
    private $date;

    /*getters and setters*/

This is some values for index, according to my database schema (example):

----------------------------------
id    | index    | dateTime      |
----------------------------------
1     | 1700.000 | dateTime datas|
----------------------------------
2     | 1200.000 | dateTime datas|
----------------------------------
3     | 1200.000 | dateTime datas|
----------------------------------
4     | 1304.000 | dateTime datas|
----------------------------------
etc...| etc...   | etc...        |

I have this method into a controller:

$em=$this->getDoctrine()->getManager();


$queryIndex = $em->createQuery( 'SELECT g.index
                                    FROM MySpaceMyBundle:Graphique g');

$array = array_map('current', $queryIndex);

$response = new Response();
$data = json_encode($array);
$response->headers->set('Content-Type', 'application/json');
$response->setContent($data);

return $response;

it returns me this into my json response:

["1700.000","1200.000","1200.000","1304.000","1800.000","2012.000","2048.000","1048.000","3000.000","5421.000"]

but I need to have this simple array result (instead the json response I give you just above):

[1700.000,1200.000,1200.000,1304.000,1800.000,2012.000,2048.000,1048.000,3000.000,5421.000]

I need to return a simple array in my json response in order to have this decimal values for displaying them into a highchart graphic.

How can I proceed? I already try some Doctrine methods like ->getArrayresult(), ->getScalarResult(), ->toArray(), but the results are the same. I need to make my query result to a simple array.

3
  • 1
    json_encode($array, JSON_NUMERIC_CHECK); try this one. Commented May 4, 2015 at 11:03
  • @insertusernamehere, for 'current', is the same thing as ` array_map(function($value) { return $value['index']; }, $resultIndex);` Commented May 4, 2015 at 11:14
  • @DawidSajdak, please make an answer, your solution is the good one, you're the best. That's what I need to have my values without the double quotes! Commented May 4, 2015 at 11:15

1 Answer 1

2
json_encode($array, JSON_NUMERIC_CHECK); 

This solution should help you.

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.