Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twig check if there are any values in the array

Tags:

php

twig

symfony

How can I check if any values are provided to the array. In PHP I'm adding to the array like this:

$myArray['index1'] = $someVal1;
$myArray['index2'] = $someVal2;

The problem is that when in Twig I use |length filter it gives results when $someVal1 or $someVal2 have no values (these are values taken from the form so they don't have to be filled). So I want to check if the values are not provided in the whole array, so:

{% if myArray|what_filter_here? == 0|empty|whatever %} This text should not appear {% endif %}

Can it be done in one single condition?

like image 226
FreakSoft Avatar asked Dec 09 '15 09:12

FreakSoft


2 Answers

Something like:

{% if myArray|length > 0 %}
    This text should not appear 
{% endif %}
like image 154
Horaland Avatar answered Nov 09 '22 08:11

Horaland


Try with empty -

{% if myArray is empty %} ... {% endif %}
like image 45
Sougata Bose Avatar answered Nov 09 '22 10:11

Sougata Bose