Twig中的PHP关联数组循环

时间:2018-11-27 21:38:50

标签: php twig

我如何在Twig中循环此数组? 我在做什么错了?

这是我所拥有的

<?php foreach (array('price','weight','length','width','height','points') as $mod) { ?>
<label class="col-sm-2 control-label" for="input-<?php echo $mod . $option_row; ?>"><?php echo ${'text_option_'.$mod}; ?></label>
  <select name="product_option[<?php echo $option_row; ?>][value][<?php echo $mod; ?>_prefix]" class="form-control">
<option value=""<?php if (isset($product_option['value'][$mod.'_prefix']) && $product_option['value'][$mod.'_prefix'] == '') echo ' selected'; ?>>NONE</option>
<option value="+"<?php if (isset($product_option['value'][$mod.'_prefix']) && $product_option['value'][$mod.'_prefix'] == '+') echo ' selected'; ?>>+</option>
</select>

这是我做过的但没有做的事情

{% for price, weight, length, width, height, points in mod %}
<label class="col-sm-2 control-label" for="input-{{ mod . option_row}}">{{ text_option_ . mod}}</label>
<select name="product_option[{{ option_row }}][value][{{ mod }}_prefix]" class="form-control">
<option value=""{% if product_option['value'][mod.'_prefix'] is defined and product_option['value'][mod.'_prefix'] == '' %} {% endif %} {{'selected'}}; >NONE</option>
<option value=""{% if product_option['value'][mod.'_prefix'] is defined and product_option['value'][mod.'_prefix'] == '+' %} {% endif %} {{'selected'}}; >+</option>
</select>
{% endfor %}

1 个答案:

答案 0 :(得分:0)

您在构建阵列时出错。而且它是{% for needle in haystack %}中的twig ...所以您的第一行应该变成:

{% for mod in [ 'price','weight','length','width','height','points' ] %}