1

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"
      }
    ]
  },
]
3
  • And what's the exact problem? I don't see any twig codes. Commented Aug 3, 2019 at 13:02
  • And what is items? Commented Aug 3, 2019 at 13:08
  • Sorry, I copy-pasted wrong code, please check it now. thanks Commented Aug 3, 2019 at 13:14

1 Answer 1

3

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 %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.