数组

时间:2016-02-11 12:08:40

标签: php arrays symfony twig

如何表示以下格式给出的数组数据:

  array(3) { 
  [0]=> array(4) {["cohort"]=5 ["total"]=5 ["week"]=1 ["active"]=2 } 
  [1]=> array(4) {["cohort"]=5 ["total"]=5 ["week"]=2 ["active"]=1 } 
  [2]=> array(4) {["cohort"]=6 ["total"]=3 ["week"]=1 ["active"]=1 } 
  }

在这样的树枝上展示:

  cohort| total| 1 (week) | 2 (week) 
  ------------------------------------
   5    |  5   | 2        | 1 
   6    |  3   | 1        | -

我的问题是,我不知道如何循环遍历数组,因此我只得到行号等于唯一的同类群号(只有5和6,而不是5,5,6),得到'周'作为列名称的数字和要在相关“周”列和相关群组行中显示的“有效”值

我试着这样做:

<table>
<tr><td>Cohort</td><td>Registered users</td>
    {% for week in logged_users %}
        {% if week.week_number not in weekArray %}
            <td>{{week.week_number}}</td>
            {% set weekArray = weekArray|merge([week.week_number]) %}
        {% endif %}
    {% endfor %}
</tr>
{% for data in logged_users %}
<tr>
    {% if data.cohort not in cohortArray %}
        <td>{{data.cohort}}</td>
        <td>{{ data.total_users }}</td>
        {% for active in logged_users %}
            <td>{{ active.active_users }}</td>
        {% endfor %}
    {% set cohortArray = cohortArray|merge([data.cohort]) %}
    {% endif %}
</tr>
{% endfor %}
</table>

但结果我得到了这个:

enter image description here

1 个答案:

答案 0 :(得分:0)

<table>
<thead>
<tr><th>cohort</th><th>total</th><th>1 - week</th><th>2 - week</th></tr>
</thead>
<tbody>
{% for i,a in data %}
<tr>
<td>{{ a.cohort }}</td><td>{{ a.total }}</td><td>{{ (a.week==1 ? a.active : '-') }}</td><td>{{ (a.week==2 ? a.active : '-') }</td>
</tr>
{% endfor %}
</tbody>
</table>