|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
robb84 Smarty n00b
Joined: 02 Sep 2020 Posts: 1
|
Posted: Wed Sep 02, 2020 3:49 pm Post subject: Count total occurrences in subarray |
|
|
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:
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Fri Sep 04, 2020 7:35 pm Post subject: |
|
|
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 |
|
|
|
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
|