一个循环中的多个数组

Multiple arrays in one loop

我目前正在开发 Symfony 2.7,我的控制器中有 3 个阵列。但我想在视图中的一个数组中显示所有这 3 个数组。有人知道

您可以在我的控制器中看到 return 和我的 3 个数组(实体、tabStatus 和 tabName)我无法将数组合并为一个。

return $this->render('testAdminBundle:Default:showBt.html.twig', 
array('entities' => $entities, 'tabStatus' => $tabStatus, 'tabName' => $tabName));

这是我的观点:

   <div class="bs-example">
        <table class="table table-striped">
          <thead>
            <tr>
              <th>Name</th>
              <th>Login</th>
              <th>Status</th>
              <th>Items</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
          {% for value in entities %}
            <tr>
              <td></td>
              <td>{{ value.login }}</td>
              <td></td>
              <td>{{ value.items }} / 2400</td>
              <td></td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div><!-- /example -->
      <br>

你可以在我的视图中看到我只显示了数组实体,因为我不能将其他 2 个数组放在 for 中。有可能做这样的事情: {% for value1 in entities, value2 in tabStatus, value3 in tabName %} ?或者也许我需要在树枝中使用密钥?

提前致谢!

PokeRwOw

尝试这样的事情:

{% for key, value in entities %}
    {{ value }} {{ tabStatus[key] }} {{ tabName[key] }}
{% endfor %}

为什么不能将它们合并为一个?你试过 array_merge_recursive 而不是 array_merge 吗?否则 Alexander Micklewright 解决方案应该是你想要的。或使用多个 foreach 并使用 includes 来避免重复代码。