array_join

The `array_join` directive combines elements from an array-typed variable into a single string, using a specified separator.

Syntaxarray_join [to=$target_var];
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments2-3

Description

The array_join directive in NGINX is designed to concatenate the elements of an array variable into a single string, separated by user-defined delimiters. This directive is part of the 'Array-typed variables for NGINX' module, which enables the use of array-like data structures within NGINX configurations. It allows users to take an array variable and create a composite string that can be used in further processing or output.

To use array_join, you must specify a separator character or string that will be placed between the elements of the array. Optionally, you can specify a target variable to store the resulting string. When the directive is invoked, it iterates through the elements of the designated array variable and concatenates them into a single string, applying the specified separator.

This directive is particularly useful in scenarios where you need to create an SQL query or other string-based conditions from list values. For instance, you might gather a list of filenames or other identifiers into an array and then construct a database condition or a similar output string using array_join. Note that this directive should be placed in an appropriate context, such as within an http, server, or location block, and it also supports nested usage within conditional blocks.

Config Example

location /foo {
    array_split ',' $arg_files to=$array;
    array_join ' or ' $array to=$sql_condition;
    echo "select * from files where $sql_condition";
}

Ensure that the array variable is initialized and populated before using array_join; otherwise, it will result in an empty string.

Using array_join directly in contexts where an array variable isn't defined will lead to configuration errors.

Be mindful of the array size as very large arrays could produce excessively long concatenated strings, potentially causing performance issues.

← Back to all directives