繁体   English   中英

在Twig中循环二维数组

[英]Loop a two dimensional array in Twig

我搜索了很多以找到答案。 但我不能使用其中任何一个。

所以,我有一个数组

array (size=1)
  0 => 
    array (size=4)
      'id' => string '1' (length=1)
      'name' => string 'Category' (length=8)
      'totalposts' => string '0' (length=1)
      'description' => string 'Twig test 123456789123456789' (length=28)

但是我无法在Twig上显示它。
这是我的Twig代码:

{% for category in categories %}
                <a class="list-group-item" href="kategori.php?id={{ category[loop.index].id }}">
                    <h4 class="list-group-item-heading">{{  category[loop.index].name }}<small class="pull-right">{{ category[2] }} {% trans " konu." %}</small></h4>
                    <p class="list-group-item-text"><?php echo $row['description']?></p>
                </a>
        {% else %}
            <a class="list-group-item list-group-item-danger">
                <h4 class="list-group-item-heading"><?php echo _('Hiç bir kategori bulunamadı.') ?></h4><br>
                <?php if ( isset($_SESSION['privledge']) && $_SESSION['privledge'] == 10) : ?>
                <p class="list-group-item-text"><? echo _('Hemen yeni bir kategori yaratabilirsin!') ?></p>
                <?php endif;?>
            </a>
        {% endfor %}

这是我的PHP代码:

$catArray = array();
$index = 0;

$result = $connection->query('SELECT * FROM category ORDER BY id ASC;');

while ($row = $result->fetch_assoc()) {
    $catArray[$index] = $row;
    $index++;
}

echo $twig->render('index.twig', array(
    'categories' => $catArray));

我应该怎么做才能显示Twig上的数据

您是否尝试过删除loop.index片段和仅引用类别,而该类别应该归结为关联属性?

{{ category.name }}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM