【发布时间】:2019-01-11 02:57:36
【问题描述】:
大家好,我正在尝试将数组传递给 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,
));
}
ps / 编辑:日期是图形的,看起来像这样:
希望我解释清楚,谢谢所有尝试回答的人:p
使用 DarkBee 方法我得到了这个:
【问题讨论】:
标签: javascript php arrays type-conversion twig