尝试将 DQL 查询放入 Highcharts 数组,returns 0 尽管数组已填充

Trying to put DQL query into Highcharts array, returns 0 despite array being populated

这是我在代码中查询的var_dump:

array (size=2)
  0 => 
    array (size=4)
  0 => 
    array (size=1)
      'barbell_curl_1_weight' => int 15
  1 => 
    array (size=1)
      'barbell_curl_1_weight' => int 20
  2 => 
    array (size=1)
      'barbell_curl_1_weight' => int 25
  3 => 
    array (size=1)
      'barbell_curl_1_weight' => int 20

这是我的查询:

/**
 * @param User $user
 * @param String $col
 * @return array
 */
public function getStatsForStatsByDQL(User $user,  String $col)
{

    $parameters = (array('user' => $user,
    ));
    $em2 = $this->getDoctrine()->getManager()
        ->getRepository('AppBundle:ExerciseStats')
        ->createQueryBuilder('g')
        ->setParameters($parameters)
        ->where('g.user = :user')
        ->select('g.'. $col)
        ->setMaxResults(12)
        ->join('g.user', 'user')
        ->orderBy( 'g.'. 'timestamp','ASC')
        ->getQuery()->getResult(Query::HYDRATE_ARRAY);
    return $em2;
  }

正如您从 var_dump 看到的那样,我正在获取我需要的数据。这是它包含的 Highcharts 函数:

/**
 * @return \Symfony\Component\HttpFoundation\Response
 * @Route ("user/stats", name="user_stats")
 */
public function chartAction()
{
    $user = $this->getUser();
    // Chart
    $query = $this->getStatsForStatsByDQL($user, 
ExerciseConsts::BARBELL_CURL_1_WEIGHT);


    $series = array($query, array("name" => "weight", "data" => 
$query));

    $ob = new Highchart();
    $ob->chart->renderTo('linechart');  
    $ob->title->text('Barbell Curl Weight');
    $ob->xAxis->title(array('text'  => "Workouts"));
    $ob->yAxis->title(array('text'  => "Weight"));
    $ob->series($series);

    return $this->render('user/stats.html.twig', array(
        'chart' => $ob
    ));
}

所以我得到的只是 0。在 xdebug 中使用断点时,我得到了这个:

1 = {array} [2]
  name = "weight"
  data = {array} [4]
  0 = {array} [1]
    barbell_curl_1_weight = 15
  1 = {array} [1]
    barbell_curl_1_weight = 20
  2 = {array} [1]
    barbell_curl_1_weight = 25
  3 = {array} [1]
    barbell_curl_1_weight = 20

如何提取权重并将其放入 Highchart 图表中?在这方面完全缺乏 Highcharts Bundle 的文档。让我知道是否有人有任何线索。谢谢!

这是 json_encode($series, JSON_NUMERIC_CHECK) 的 var_dump。也许这会有所帮助:

字符串'[[{"barbell_curl_1_weight":15},{"barbell_curl_1_weight":20},{"barbell_curl_1_weight":25},{"barbell_curl_1_weight":20}], {"name":"weight","data":[{"barbell_curl_1_weight":15},{"barbell_curl_1_weight":20},{"barbell_curl_1_weight":25}, {"barbell_curl_1_weight":20}]}]' (长度=266)

我找到了解决方案。当我们处理多维数组时,Highcharts 不会向下钻取到第一个数组之外,returns 0。这是我对每个数组的值 return 的迭代:

 $temp = array();
    foreach($array as $k=>$v){
        if(is_array($v)){
            foreach($v as $key=>$value){
                $temp[] = $value;
            }
        }
    }

所以现在,我只需要将 $temp 替换为 $query,瞧瞧,图表会正确呈现所有值。