在Twig中循环二维数组

时间:2015-09-03 20:29:34

标签: php arrays 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上显示数据

1 个答案:

答案 0 :(得分:1)

您是否尝试删除loop.index片段并仅引用类别,这应该只是关联属性?

{{ category.name }}
相关问题