使用 Smarty 计算子数组中的总出现次数

Count total occurrences in subarray with Smarty

我有这个数组:

[attributes] => Array
            (
                [1] => Array
                    (
                        [id_attribute] => 1
                        [id_attribute_group] => 1
                        [name] => Rosso
                        [group] => Colore
                        [reference] => WD1012
                        [ean13] => 
                        [isbn] => 
                        [upc] => 
                    )

                [9] => Array
                    (
                        [id_attribute] => 24236
                        [id_attribute_group] => 9
                        [name] => 31 x 86.5 cm.
                        [group] => Dimensioni Fiore 1
                        [reference] => WD1012
                        [ean13] => 
                        [isbn] => 
                        [upc] => 
                    )

                [10] => Array
                    (
                        [id_attribute] => 24237
                        [id_attribute_group] => 10
                        [name] => 31 x 71.5 cm.
                        [group] => Dimensioni Fiore 2
                        [reference] => WD1012
                        [ean13] => 
                        [isbn] => 
                        [upc] => 
                    )

                [11] => Array
                    (
                        [id_attribute] => 24238
                        [id_attribute_group] => 11
                        [name] => 32.5 x 88 cm.
                        [group] => Dimensioni Fiore 3
                        [reference] => WD1012
                        [ean13] => 
                        [isbn] => 
                        [upc] => 
                    )

            )

我想统计[组]的值,同时搜索它的值一个词:

{foreach from=$variants item=variant}
    {if $variant.group|stristr:'Dimensioni'}
        {$variant.group} 
    {/if}
{/foreach}

输出:

Dimensioni Fiore 1
Dimensioni Fiore 2
Dimensioni Fiore 3

现在我想计算数组中的总出现次数(应该是 3):

{$variant.group|count}

输出:

1
1
1

我必须计算出现次数,就好像它们是 2 次或更多次一样,它必须做一些事情,但输出计数每个数组只出现 1 次,而不是总数。 实现这一目标的方法是什么?

谢谢。

您可以使用 counter 来做到这一点:

{counter print=false start=0 name=Dimensioni assign=dimensioniCount}
{foreach from=$variants item=variant}
    {if $variant.group|stristr:'Dimensioni'}
        {$variant.group}
        {counter name=Dimensioni}
    {/if}
{/foreach}

{if $dimensioniCount>=2}
    <h1>Do something here</h1>
{/if}