最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array to string conversion, Php to js - Stack Overflow

programmeradmin10浏览0评论

Hello guys i am trying to pass an array to a js file but i have this error :

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion").

Soo this what i want : data: ['2017/7','2017/8']

so i do it like this (in twig) : data: '{{ user_month_month }}'

And when i vardump user_month_month this is what i get :

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

So i think that i don't call it well in the js part, what should i do ?

if you want there is my controller :

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 / edit : the date is for a graphic and it looks like this :

Hope that i explain well, thx for all who will try to answer :p

With DarkBee method i got this :

Hello guys i am trying to pass an array to a js file but i have this error :

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion").

Soo this what i want : data: ['2017/7','2017/8']

so i do it like this (in twig) : data: '{{ user_month_month }}'

And when i vardump user_month_month this is what i get :

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

So i think that i don't call it well in the js part, what should i do ?

if you want there is my controller :

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 / edit : the date is for a graphic and it looks like this :

Hope that i explain well, thx for all who will try to answer :p

With DarkBee method i got this :

Share Improve this question edited Aug 3, 2018 at 9:33 A beginner asked Aug 3, 2018 at 8:54 A beginnerA beginner 1242 silver badges15 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

The fastest way to pass an array from twig to javascript is by converting the array to JSON, twig has a builtin filter json_encode for this:

data: {{ user_month_month | json_encode | raw }}

The remended way by symfony for passing information from twig to javascript is by storing information in data attributes and reading them later in JavaScript. Documentation here.

In your case that you have an array you should use this to work as expected:

<div id="your-div" data-yourArray="{{ yourArray|json_encode|e('html_attr') }}">

then use JSON.parse to deserialize your array in javascript.

DarkBee's answer does not include the use of the escape filter, that's why it doesn't work as expected.

In your javascript code:

var yourDiv = document.getElementById('your-div');
var yourArray = JSON.parse(yourDiv.dataset.yourArray); 
<input type="text" id="txt"> <!-- // 2017/7 2017/8 -->

<script>
    date = '{{ user_month_month[0] ~ ' ' ~ user_month_month[1] }}';
    alert(date); // 2017/7 2017/8
    txt = document.getElementById('txt');
    txt.value = "{{ user_month_month[0] ~ ' ' ~ user_month_month[1] }}";
</script>
发布评论

评论列表(0)

  1. 暂无评论