4

I want to use an expression to select features matching the values of an array.

Select features from a list is done by the IN operator:

"field" IN ('value1','value2')

But how to select features from an array like ['value1','value2']? Do I have to convert it to a list (somehow) or is there a "direct" method of doing so?

QGIS 3.10

2
  • 1
    array_find(['value1','value2'], "field") <> -1 returns 0 (True) if field in array. Dec 9, 2019 at 13:30
  • 1
    You can also use array_contrains. array_contains(array(5,7,9),5) = True returns 1 (True) array_contains(array(5,7,9),6) = True returns 0 (False) Dec 9, 2019 at 13:38

1 Answer 1

4

The expression builder has a whole section of functions for dealing with arrays.

array Returns an array containing all the values passed as parameter

array_append Returns an array with the given value added at the end

array_cat Returns an array containing all the given arrays concatenated

array_contains Returns true if an array contains the given value

array_distinct Returns an array containing distinct values of the given array

array_filter Returns an array with only the items for which an expression evaluates to true

array_find Returns the index (0 for the first one) of a value within an array. Returns -1 if the value is not found.

array_first Returns the first value of an array

array_foreach Returns an array with the given expression evaluated on each item

array_get Returns the Nth value (0 for the first one) of an array

array_insert Returns an array with the given value added at the given position

array_intersect Returns true if any element of array_1 exists in array_2

array_last Returns the last element of an array

array_length Returns the number of elements of an array

array_prepend Returns an array with the given value added at the beginning

array_remove_all Returns an array with all the entries of the given value removed

array_remove_at Returns an array with the given index removed

array_reverse Returns the given array with array values in reversed order

array_slice Returns the values of the array from the start_pos argument up to and including the end_pos argument

array_to_string Concatenates array elements into a string separated by a delimiter and using optional string for empty values

generate_series Creates an array containing a sequence of numbers

regexp_matches Returns an array of all strings captured by capturing groups, in the order the groups themselves appear in the supplied regular expression against a string

string_to_array Splits string into an array using supplied delimiter and optional string for empty values

(Source: QGIS User Manual)

Use array_contains(your_array, "field") to test whether your array contains the current field value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.