数组到字符串转换,Php到js

A b*_*ner 0 javascript php arrays type-conversion twig

大家好我想尝试将数组传递给js文件,但我有这个错误:

在渲染模板期间抛出异常("注意:数组到字符串转换").

这就是我想要的: data: ['2017/7','2017/8']

所以我这样做(在树枝上): data: '{{ user_month_month }}'

当我vardump user_month_month这是我得到的:

array(2) { [0]=> string(6) "2018-7" [1]=> string(6) "2018-8" }

所以我认为我不会在js部分称它为好,我该怎么办?

如果你想要我的控制器:

public function GraphicShow()
{
    $em = $this->getDoctrine()->getManager();

    $users = $em->getRepository('AppBundle:User')->countUsers();
    $not_logged = $em->getRepository('AppBundle:User')->countNotActiveUsers();
    $logged = $em->getRepository('AppBundle:User')->countActiveUsers();
    $user_month_months = $em->getRepository('AppBundle:Profile')->countByMonthMonth();
    $user_month_totals = $em->getRepository('AppBundle:Profile')->countByMonthTotal();

    $array_totals = array();
    $array_months = array();

    foreach($user_month_totals as $user_month_total) {
        $array_totals[] = intval($user_month_total["total"]);
    }

    foreach ($user_month_months as $user_month_month) {
        $array_months[] = $user_month_month["month"];
    }

    $not_logged_result = $not_logged["number"] / $users["number"] * 100;
    $logged_result = $logged["number"] / $users["number"] * 100;

    var_dump($array_months);
    die();

    return $this->render('admin/user/pie_stats.html.twig', array(
        'user_month_month' => $array_months,
        'user_month_total' => $array_totals,
        'user_not_logged' => $not_logged_result,
        'user_logged' => $logged_result,
        'users' => $users,
        'loggedAs' => $this->getUser(),
        'alert' => 0,
    ));
}
Run Code Online (Sandbox Code Playgroud)

ps/edit:日期是图形,它看起来像这样:

在此输入图像描述

希望我能为所有想要回答的人解释得很好:p

使用DarkBee方法,我得到了这个:

在此输入图像描述

Dar*_*Bee 6

从传递数组的最快方式twigjavascript是由阵列转换为JSON,twig具有内置过滤器json_encode此:

data: {{ user_month_month | json_encode | raw }}
Run Code Online (Sandbox Code Playgroud)