Bat*_*ico 4 php arrays array-key symfony twig
我在twig编码,以显示我从PHP获得的值,我希望数组的键名出现在值的上方.
{% if user.address %}
<tr>
{% for address in user.address %}
{% for parts in address %}
<td width="25%">
{{ parts }}
</td>
{%endfor%}
{% endfor %}
</tr>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
在{% for address in user.address %}我想要的部分 ({{address.key}}或获得数组的键名所需的真实句子)
数组如下:
-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"
]
]
Run Code Online (Sandbox Code Playgroud)
编辑:
Ty的帮助结果是:
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
你可以试试这个:
{% for key, address in user.address %}
Run Code Online (Sandbox Code Playgroud)
这样你就拥有了关键和价值