douba9425 2016-05-13 13:10 采纳率: 100%
浏览 89
已采纳

三个嵌套的for循环似乎不起作用

I'm currently working on a website project on Symfony 2.8. But I have a problem : I want to display information from a JSON object. However, I need to do three nested for-loops in order to do what I want.

So here's my code chunks :

The array I want to browse :

element stdClass Object =>
(
[partschemes] => Array
(
    [0] => ( [id] => 1 )
)
[decodedPartitions] => Array
    (
        [0] => stdClass Object
            (
                [partitions] => Array
                    (
                        [0] => stdClass Object
                            (
                                [name] => WINDOWS
                                [type] => primary
                                [size] => -
                                [filesystem] => fat32
                            )

                        [1] => stdClass Object
                            (
                                [name] => DATA*
                                [type] => primary
                                [size] => 256 Mo
                                [filesystem] => fat32
                            )

                    )

            )

    )
)

My twig template :

{% for i, disk in element.decodedPartitions %}
        <tr>
            <th class="text-center" rowspan="3" width="10%">
                <a href="{{ app.request.baseUrl }}/partscheme/details/{{ element.partschemes[i].id }}" class="btn btn-info" title="{{ 'button.details' | trans }}">Disk {{ i }}</a>
            </th>
        </tr>

        {% for j, part in disk.partitions %}
        <tr>
            <th class="text-center" width="10%">Partition {{ j }}</th>
            <td>
                {% for k, partInfo in part %}
                    {{ k }}: {{ partInfo }}<br>
                {% endfor %}
            </td>
        </tr>
        {% endfor %}
    {% endfor %}

And as a result, the generated page shows me that, without information of different partitions:

---------------------------------
       | Partition 0 |          |
Disk 1 |-------------|----------|
       | Partition 1 |          |
---------------------------------
  • 写回答

1条回答 默认 最新

  • dpbe81245 2016-05-13 14:16
    关注

    Thanks to Miro, I was able to iterate over my JSON object.

    The reason was that it is not possible to iterate over an stdClass object directly in Twig. So I added a parameter in my json_decode use in the controller.

    To convert a JSON into an stdClass object : json_decode($json)

    To convert a JSON into an associative array : json_decode($json, true)

    More information about json_decode.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?