Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Count total occurrences in subarray

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
robb84
Smarty n00b


Joined: 02 Sep 2020
Posts: 1

PostPosted: Wed Sep 02, 2020 3:49 pm    Post subject: Count total occurrences in subarray Reply with quote

I have this array:

Code:
    [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] =>
                        )

                )


I want to count the [group] value while searching its value a word:

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


Output:

Dimensioni Fiore 1
Dimensioni Fiore 2
Dimensioni Fiore 3

Now I want to count the total occurrences inside arrays (should be 3):

Code:
{$variant.group|count}


output:
Code:

    1
    1
    1


I have to count the occurrences as if they are 2 or more, it must does something, but the output count only 1 occurrence for each array and not the total.
What's the way to achieving this ?

Thanks.
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Fri Sep 04, 2020 7:35 pm    Post subject: Reply with quote

You are asking to simply print {$variant.group} when the test succeeds.

So, before the test, initialize a counter: {$groupCounter = 0}
In the test, add to the counter: {$groupCounter = $groupCounter + 1}
Then do something with the result: {$groupCounter}
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP