Twig中的php输出数组

时间:2019-05-28 18:22:18

标签: php twig

我试图在Twig中输出数组,但是我在Twig中没有足够的经验。

如果键是索引,我只是不知道如何输出非关联数组 数组

的$ c_条评论
Array
(
    [0] => Array
        (
            [0] => 20:28 28:05:2019
            [1] => dokl
            [2] => 45645
        )

    [1] => Array
        (
            [0] => 20:27 28:05:2019
            [1] => Alex
            [2] => 546
        )

)
    {% for vars.item.c_page in vars.item.comment %}
       <li>{{ vars.item.comment. }}</li>
    {% endfor %}

这就是我在php中显示数组的方式,在树枝中也需要

<div class="c_comments"><i>'.$c_comments[$i][0].' '.$c_comments[$i][1].' Wrote:</i><br>'.$c_comments[$i][2].'</div>';

因此,该模板根本无法工作。 因此,将数据传输到模板

$item = ['first' => $number_f, 'two' => $number_t, 'comment' => $c_comments, 'c_page' => $c_page,'pagination' => $varPagination];
$this->registry['template']->set('item', $item);

我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

如果变量为c_comments,则可以使用以下for循环:

// PHP
$data = [
    [
        '20:28 28:05:2019',
        'dokl',
        45645,
    ],

    [
        '20:27 28:05:2019',
        'Alex',
        546,
    ]
];

echo $twig->render('index.html', ['c_comments' => $data]);
<!-- index.html -->
{% for key, comment in c_comments %}
    <i>{{ comment[0] }} {{ comment[1] }} Wrote: {{ comment[2] }}</i><br>
{% endfor %}