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 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题