How to get the key name of an array in a branch view

Im coding in a branch to visualize the values ​​that I get from php, and I want the key name of the array to appear above the values.

{% if user.address %}
        <tr>
            {% for address in user.address %}
                {% for parts in address %}
                    <td width="25%">
                        {{ parts }}
                    </td>  
                {%endfor%}
            {% endfor %}
        </tr>              
{% endif %}

In the part under {% for address in user.address %}I want to put ( {{address.key}}or the real sentence needed to get the array key)

The array is like:

  -address : array:4 [▼
    "Door" => array:1 [▼
      0 => "225"
    ]
    "Street" => array:1 [▼
      0 => "Pinky street"
    ]
    "District" => array:1 [▼
      0 => "District north"
    ]
    "City" => array:1 [▼
      0 => "New York"
    ]
  ]

Edit:

Ty for help:

{% if user.address %}
        <tr>
           {% for key, address in user.address %}
                    <td width="25%">
                        {{ key }}
                    </td>
                {%endfor%}
        </tr> 
        <tr>
           {% for address in user.address %}
                {% for parts in address %}
                    <td width="25%">
                        {{ parts }}
                    </td>  
                    {%endfor%}
           {%endfor%}
        </tr>
    {% endif %}
+4
source share
2 answers

you can try the following:

{% for key, address in user.address %}

So you have a key and a value

+5
source

try it

{% for key, user in users %}
    <li>{{ key }}: {{ user.username|e }}</li>
{% endfor %}

and here is the documentation of Iteration on Keys and Values

+3
source

Source: https://habr.com/ru/post/1689890/


All Articles