渲染数组中的单个数据

时间:2018-08-31 12:33:17

标签: php twig symfony-3.2

{{dump(states)}}返回

FormView {#5921 ▼
  +vars: array:33 [▼
    "value" => []
    "attr" => []
    "form" => FormView {#5921}
    "id" => "States"
    "name" => "States"
    "full_name" => "States"
    "disabled" => false
    "compound" => false
    "method" => "POST"
    "action" => ""
    "preferred_choices" => []
    "choices" => array:41 [▶]
    ]
  +parent: FormView {#5618 ▶}
  +children: []
  -rendered: false
  -methodRendered: false
}

我想显示选择状态-选项35。如何从具有41个选择的数组中取出单个选择?

1 个答案:

答案 0 :(得分:1)

您可以使用 [] 语法访问树枝中的数组成员。

因此,您可以在树枝中以如下方式呈现选择数组的第35个选择:

{{ states.vars.choices[34] }}

有关如何访问树枝中的数组成员的更多信息,请查看this article from knpuniversity