CodeClerks

How can I count an array in Smarty?



Write the reason you're deleting this FAQ

How can I count an array in Smarty?

I have this array in PHP being passed to smarty:
unansweredTopics_arr

I want to create an if statement and if there is a count of more than 0 items, display the contents of the array by looping through it. If there are no items, I want to display some generic text like "No Topics Found".

Is there a way to count the items in an array via Smarty?

Comments

Please login or sign up to leave a comment

Join
jakemadness
The best solution I can think of would be to use the smarty {php}{/php} tags and create a function to call like
function post_count($x){ }

I don't know Smarty as well as I would like and still learning it. How can I count an array in Smarty?



Are you sure you want to delete this post?

anasman
With PHP 5.5 and Smarty 3 you can use new functions:
{
$type_count = array_count_values(array_column($mydata,'type'))
}
Count of type 2: {
$type_count['2']
}



Are you sure you want to delete this post?

jakemadness
As of smarty 3 you can use the following
{count($my_array)}
 
or you can use
{$my_array|count}
in smarty 2 or 3, personally I like rain TPL {function="count($my_array)"}



Are you sure you want to delete this post?

anasman
smarty.incutio.com/?page=SmartyFrequentlyAskedQuestions#template-1
check this link



Are you sure you want to delete this post?