树枝中的PHP数组

时间:2018-04-28 10:21:24

标签: php arrays twig opencart opencart-3

我在php中有这个代码,我用树枝转换它但没有得到相同的结果。

 <?php for($j=0;$j<sizeof($products[$category]);) { ?>
   <div class="wrapper">
       <?php for($f=0;$f<$rows;$f++,$j++) { 
                if($j<sizeof($products[$category])) { ?>
                    <?php if($products[$category][$j]) { ?>

                    <?php } 
                     if(sizeof($products[$category])<(4*$rows)-1){
                       if(((sizeof($products[$category])-($j)) + 1)%3 == 0){
                              $cnt = 1;
                                }
                                if($cnt == 1){
                                        $f++;
                                }
                            }
                        ?>
                        <?php } ?>
                        <?php } ?>
                    </div>

在twig文件中:

{% for j in 0..(products[category]|length)-1 %}
    <div class="wrapper">
          {% for f in 0..rows-1 %}
                {% if j < products[category]|length %}
                    {% if products[category][j] %}

                    {% endif %} 
                    {% if products[category]|length < ((4*rows)-1) %} 
                        {% if(((products[category]|length)-(j)) + 1)%rows == 0 %}
                                    {% set cnt = 1 %}
                        {% endif %}
                        {% if cnt == 1 %}
                                {% set f = f + 1 %}
                        {% endif %}
                    {% endif %}
                 {% endif %}
            {% set j = j + 1 %}
           {% endfor %}  

    </div>

如何使用for循环f迭代实现$j++

请帮忙。

提前致谢。

2 个答案:

答案 0 :(得分:2)

试试这个会对你有用

<div class="wrapper">
{% set j = 0 %}
{% for f in 0..rows-1 %}
    {% if j < products[category]|length %}
        {% if products[category][j] %}

        {% endif %} 
        {% if products[category]|length < ((4*rows)-1) %}
            {% if(((products[category]|length)-(j)) + 1)%rows == 0 %}
                {% set cnt = 1 %}
            {% endif %}
            {% if cnt == 1 %}
                {% set f = f + 1 %}
            {% endif %}
        {% endif %}
    {% endif %} 
    {% if f < rows %}
        {% set j = j + 1 %}
    {% endif %}
{% endfor %}
</div>

答案 1 :(得分:0)

根据你想要的输出写下面的代码..

Like - Total Rows 2,item is 6 then

1  3  5
2  4  6

行为3,项目为8

1  4  7
2  5  8
3  6

假设twig follow-zero based index array

{% set TOTALITEMS = (products[category]|length) %}
{% set TOTALROWS = 2 %}
{% set ITEMSPERROW = TOTALITEMS / TOTALROWS %} <!-- make sure you get integer number only, if not then make it by helper functions of php / twig -->

{% for rowIndex in 0 .. TOTALROWS - 1 %}
  <!-- start a div which will contain a row -->

    {% for columnIndex in 0 .. ITEMSPERROW - 1 %}

        <!-- here you have a item ready to dump in html -->
        {%  products[category][ (columnIndex * TOTALROWS) + rowIndex ]  %}

    {% endfor %} <!-- end of loop for columnIndex ...-->

  <!-- end a div which contained a row -->
{% endfor %} <!-- end of loop for rowIndex ...-->