在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上的数据

你试过放弃循环吗?索引部分和引用类别,哪一个应该归结为关联属性?

{{ category.name }}