array_map
The `array_map` directive applies a template based on each element in an array variable.
Description
The array_map directive in the NGINX array-typed variables module is designed to process each item in an array variable by applying a specified template. This directive allows NGINX to transform array values into a new format as required for subsequent processing in the configuration. The directive takes two or three parameters, where the first parameter is a template string that specifies how to transform each array element, the second is the source array variable, and the optional third parameter indicates whether to perform the operation in-place or not.
When invoked, the array_map processes the array variable and replaces $array_it with the index of the current item being processed during the mapping operation. This enables dynamic referencing of each array element while applying the specified transformation. The outcome of this transformation can be utilized for generating dynamic content responses or constructing complex database queries within the NGINX configuration.
It is important to note that the directive can be used only in contexts, including http, server, location, if in server, and if in location. This tailored behavior allows you to manipulate the array contents directly based on specific locations or conditions in the routing structure.
Config Example
location /items {
array_split ',' $arg_items to=$items;
array_map "item = $array_it" $items;
}Ensure the template string is valid and references $array_it appropriately.
Using the directive in unsupported contexts will result in a configuration error.
Remember that array variables are not usable outside of this module's directives without conversion to string using array_join.
Keep in mind that the array_map does not modify the original array; rather it creates a new transformation.