trim_array

The trim_array filter will iterate through an array and remove "unwanted" values like false, null empty array/string, strings that would be considered empty, arrays that contains "unwanted" values.

{{ ['value1', 'value2', 'value3']|trim_array|join(' ') }}

{# outputs "value1 value2 value3" #}

It ignores empty spaces:

It also ignores values not considered suitable for classes:

{{ [false, 'value1', '', 'value2', null]|trim_array|join(' ') }}

{# outputs "class1 class2" #}

It also supports iterable and \Traversable objects:

{# valueCollection = new \Tightenco\Collect\Support\Collection(['value1', 'value2', 'value3']) #}

{{ valueCollection|trim_array|join(' ') }}

{# outputs "value1 value2 value3" #}

It also merge and handle multidimensional arrays Ex.1

{{ [['', ' '], 'value2']|trim_array|join(' ') }}

{# outputs "value2" #}

Ex.2

{{ [['value1', ' '], 'value2']|trim_array|join(' ') }}

{# outputs "value1 value2" #}

Ex.3

{{ [['value1', false], ['', ' '], ['value2', 'value3']]|trim_array|join(' ') }}

{# outputs "value1 value2 value3" #}

Last updated