Twig :: where
The where
filter allows you to filter elements/objects in an array based on the value the specified key contains.
Operator | Description |
---|---|
== |
Ensure the values are equal and are the same data type |
!= |
Ensure the values are not equal; returns false if the values are the same but different data types |
> |
Greater than |
>= |
Greater than or equal to |
< |
Less than |
<= |
Less than or equal to |
~= |
Check if a string or array contains the <value> ; case-sensitive |
_= |
Check if a string or array contains the <value> ; case-insensitive |
/= |
Compare the <value> with a regular expression |
Since
0.1.0
Usage
where(key, comparison, value)
Parameters
-
(required)
key
: stringThe key name we'll be comparing
-
(required)
comparison
: stringThe available comparisons:
==
,!=
,>
,>=
,<
,<=
,~=
,_=
,/=
-
(required)
value
: mixedThe value we are searching for
Example Usage
{% set users = [
{ 'username': 'allejo', 'rank': 'admin', 'id': 1 },
{ 'username': 'alezakos', 'rank': 'admin', 'id': 2 },
{ 'username': 'the_map', 'rank': 'user', 'id': 3 }
] %}
<!-- [
{ 'username': 'allejo', 'rank': 'admin', 'id': 1 },
{ 'username': 'alezakos', 'rank': 'admin', 'id': 2 }
] -->
{% set filtered = users | where('rank', '==', 'admin') %}