douzhuangxuan3268 2018-02-27 11:18
浏览 10

php twig增量变量和连接表行

in my mysql db I got the following data:

 row   value 
--------------
|row1|   12  |
--------------
|row1|   13  |
--------------
|row2|   15  |
--------------

which I transform to the following php array($myarray):

[arr]=>
  array(2) {
    ["row1"]=>
    array(2) {
      [0]=>
      int(12)
      [1]=>
      int(13)
    }
    ["row2"]=>
    array(1) {
      [0]=>
      int(15)
    }
  }

which I display with the following code in twig:

{% for key, value in myarray %}
  <tr>
     <td>{{key}}</td>
     <td>{{value|join(', ')}}</td> 
  </tr>
{% endfor %}

so it looks like the following:

---------------
|row1| 12, 13 |
---------------
|row2| 15     |
---------------

My problem is that when the values for row1 and row2 are equal I need to show them in one table row, like the following (say that values for row2 are also 12 and 13 instead of 15):

---------------------
|row1, row2| 12, 13 |
---------------------

I tried to do that with twig variables like this:

{% set inc = 0 %}
  {% for key, value in myarray %}
    {% set foo ~ inc = [key, value] %}
    {% set inc = inc + 1 %}
  ...table html here 
  {% endfor %}

but still cannot join the rows in case arrays for 'row1' and 'row2' are equal. Any ideas how to do that? (There may be more than 2 'row1' and 'row2' in resulting array). Thank you.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器