【发布时间】:2015-07-13 16:08:04
【问题描述】:
在我的数据库中,我有一个名为 Graphique 的实体。这是以下架构:
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*/
这是索引的一些值,根据我的数据库架构(示例):
----------------------------------
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... |
我把这个方法变成了一个控制器:
$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;
它将这个返回到我的 json 响应中:
["1700.000","1200.000","1200.000","1304.000","1800.000","2012.000","2048.000","1048.000","3000.000","5421.000"]
但我需要这个简单的数组结果(而不是我刚才给你的 json 响应):
[1700.000,1200.000,1200.000,1304.000,1800.000,2012.000,2048.000,1048.000,3000.000,5421.000]
我需要在我的 json 响应中返回一个简单的数组,以便将这些十进制值显示到高图表中。
我该如何继续?我已经尝试了一些 Doctrine 方法,例如->getArrayresult()、->getScalarResult()、->toArray(),但结果是一样的。我需要将我的查询结果变成一个简单的数组。
【问题讨论】:
-
json_encode($array, JSON_NUMERIC_CHECK);试试这个。
-
@insertusernamehere,对于 'current',与 `array_map(function($value) { return $value['index']; }, $resultIndex);` 相同
-
@DawidSajdak,请回答,你的解决方案是好的,你是最好的。这就是我需要没有双引号的价值观!
标签: php arrays json multidimensional-array doctrine-orm