简体   繁体   中英

Twig nested array

I am having issue rendering a nested array in Twig. this is my PHP logic where I am able to get other fields such as name URL but not array data. please check twig code, I think I am doing something wrong there. Error: An exception has been thrown during the rendering of a template ("Notice: Array to string conversion").

foreach ($arrays as $array) {
    foreach ($array as $key => $value) {
        $allProjects[$key][] = $value;
    }
}

                      {% for project in projects %}
                        <tr>
                            <td>{{ project.name }}</td>
                            <td>{{ project.url }}</td>
                            <td>{{ project.assingedApprover }}</td>
                            <td></td>
                        </tr>
                    {% endfor %}

Json Data

    [
  {
    "name": "Resolute Energy Corporation",
    "url": "http://msu.edu",
    "assignedAprover": [
      {
        "firstName": "Joe",
        "lastName": "lastName"
      },
      {
        "firstName": "men",
        "lastName": "gen"
      }
    ]
  },
  {
    "name": "CBL & Associates Properties, Inc.",
    "url": "http://acquirethisname.com",
    "assignedAprover": [
      {
        "firstName": "Joe",
        "lastName": "lastName"
      },
      {
        "firstName": "men",
        "lastName": "gen"
      }
    ]
  },
]

As assignedAprover is array you can iterate over it with for :

{% for project in projects %}
    <tr>
        <td>{{ project.name }}</td>
        <td>{{ project.url }}</td>
        {% for aprover in project.assignedAprover %}
        <td>{{ aprover['first_name'] }} {{ aprover['last_name'] }}</td>
        {% endfor %}
        <td></td>
    </tr>
{% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM