Forums / Setup & design / Test for empty array

Test for empty array

Author Message

paul bolger

Wednesday 31 October 2007 7:12:26 pm

Is there a neat way to test if an array is empty?

I'm trying to make a heading not appear if an array of fetched items doesn't contain any items.

I realise that you could run a foreach loop and then do a has_content test on the first item, but this seems clumsy - is this the only way?

Paul Bolger

Łukasz Serwatka

Wednesday 31 October 2007 11:27:46 pm

Hi Paul,

You can test it with count() tpl operator

{* Example 1 *}
{def $my_array=array()}

{if $my_array|count()}

{else}

{/if}

{* Example 2 *}

{if $my_array|count()|gt(0)}

{else}

{/if}

Hope that helps.

Personal website -> http://serwatka.net
Blog (about eZ Publish) -> http://serwatka.net/blog

André R.

Thursday 01 November 2007 2:33:30 am

Doesn't empty arrays evaluate to false ??
like this:

{if $my_array}
{else}
{/if}

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

paul bolger

Thursday 01 November 2007 4:07:21 pm

Thanks, both work.

Paul Bolger