【问题标题】:Array to string conversion, Php to js数组转字符串,php转js
【发布时间】: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


    【解决方案1】:

    将数组从twig 传递到javascript 的最快方法是将数组转换为JSONtwig 有一个内置过滤器json_encode

    data: {{ user_month_month | json_encode | raw }}
    

    【讨论】:

    • 不工作......(好吧,就像没有错误一样工作)但得到了这个:["2018-7uot;,uot;2018-8uot;] 就像我这样做是为了一个图形第一个值应该是 2018-7 第二个 2018-8 ......但是通过这种方式,“[”中的第一个值第二个“&”,第三个“q”......
    • 有同样的事情......这样更好,因为现在我看到了图形,但我会编辑帖子,你可以看到它的作用
    • 好的,如果你想看的话,我换一张照片(比如删除“quot”这个词,但总是一行一个字母)
    • 当然你不需要在语句周围加上单引号,否则你处理的是string而不是array
    【解决方案2】:

    symfony 推荐的将信息从 twig 传递到 javascript 的方法是将信息存储在数据属性中,然后在 JavaScript 中读取它们。 Documentation here.

    如果你有一个数组,你应该使用它来按预期工作:

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

    然后使用 JSON.parse 在 javascript 中反序列化您的数组。

    DarkBee 的回答不包括使用转义过滤器,这就是它无法按预期工作的原因。

    在您的 javascript 代码中:

    var yourDiv = document.getElementById('your-div');
    var yourArray = JSON.parse(yourDiv.dataset.yourArray); 
    

    【讨论】:

    • 我把这个: var months = document.getElementById('month_data');在 js 文件中,我做 data:months (我把 id 放到 month_data 肯定)
    • 你应该使用: var monthsDiv = document.getElementById('months_div');获取我在回答中提到的添加“data-months”属性的div(在我的回答中,我将其命名为data-yourArray)。然后你使用: var months = JSON.parse(monthsDiv.dataset.months);以 javascript 数组的形式获取月份。
    【解决方案3】:
    <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>
    

    【讨论】:

    • 什么都不打印...就像我想自动的一样
    猜你喜欢
    • 1970-01-01
    • 2018-02-18
    • 2022-07-21
    • 2014-04-08
    • 1970-01-01
    • 2013-05-12
    • 2010-10-15
    • 1970-01-01
    • 2018-01-29
    相关资源
    最近更新 更多