'Loop through assotiave array from api platform

After a json_decode from api platform I get this associative array :

array:5 [▼
  "@context" => "/api/contexts/Horaires"
  "@id" => "/api/horaires"
  "@type" => "hydra:Collection"
  "hydra:member" => array:55 [▼
    0 => array:13 [▼
      "@id" => "/api/horaires/1"
      "@type" => "Horaires"
      "id" => 1
      "date" => "2022-03-21T00:00:00+00:00"
      "hour" => "1970-01-01T09:00:00+00:00"
    ]
    1 => array:13 [▼
      "@id" => "/api/horaires/2"
      "@type" => "Horaires"
      "id" => 2
      "date" => "2022-03-22T00:00:00+00:00"
      "hour" => "1970-01-01T09:00:00+00:00"
    ]
    2 => array:13 [▶]

How do I get a list in twig with id, date, hour?



Solution 1:[1]

The data you are trying to access is not directly accessible in the variable horaires that you are passing but lives in the property hydra:member.

{% for horaire in data['hydra:member'] %}
    {{ horaire.id }}
    {{ horaire.date|date('d-m-Y') }} {{ horaire.hour|date('H:i:s') }}
{% endfor %}

demo


sidenote

As the property's name hydra:member is not really a "valid" property name, you can't access it with the dot-notation, e.g. data.hydra:member. You would need to use the array notation to access it's value(s), e.g. data['hydra:member']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DarkBee