The ArrayFunctions extension creates a set of pure, Parsoid-compatible (see here) parser functions that perform operations on arrays.
Installation
Download and move the extracted ArrayFunctions folder to your extensions/ directory. Developers and code contributors should install the extension from Git instead, using:
Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.
TL;DR
ArrayFunctions defines a collection of parser functions that allow you to work with immutable lists and objects, collectively "arrays". Instead of storing these arrays in memory, they are outputted by the parser function directly, and can be passed around to templates or to other parser functions. While it is possible to create and work with arrays entirely through wikitext, the recommended approach is to use Scribunto (Lua), Semantic MediaWiki or Cargo to create an array that contains all the necessary data, and use ArrayFunctions only for formatting the array. The #af_list and #af_object parser functions should primarily be used for simple, one-off arrays.
ArrayFunctions defines many (over 30) parser functions, but the most useful ones are:
#af_get to retrieve a value, or perform some operation succincly;
#af_pipeline to create a pipeline of operations to prevent deep nesting;
ArrayFunctions has a commitment to not breaking backwards-compatibility. This means that user-facing code (e.g. parser functions, Lua fuctions and magic words) will keep working indefinitely, and changes will always be implemented in a backwards-compatible manner.
ArrayFunctions also tries to keep compatibility with older versions of MediaWiki for as long as possible, but due to the nature of extensions, this may not be possible indefinitely. In case an update of MediaWiki breaks a feature that ArrayFunctions relies on, it will be patched in a backwards compatible manner if this is possible. Otherwise, a new MAJOR version of ArrayFunctions will be released.
Compatibility Matrix
ArrayFunctions
MediaWiki
PHP
Scribunto1
Semantic MediaWiki1
Cargo1
1.15.0+
MediaWiki version:
≥ 1.35
PHP version
≥ 7.4.0
Scribunto version
≥ 1.35
Semantic MediaWiki version
≥ 4.0
Cargo version
≥ 3.0
1.14+
MediaWiki version:
≥ 1.35
PHP version
≥ 7.4.0
Scribunto version
≥ 1.35
Semantic MediaWiki version
≥ 4.0
not applicable
1.0-1.13
MediaWiki versions:
1.35 – 1.43
PHP version
≥ 7.4.0
Scribunto version
≥ 1.35
not applicable
not applicable
1: Soft dependency; version constraint only applicable when the software is installed.
Performance limits
ArrayFunctions version
≥ 1.16
MediaWiki already limits the total size of arrays through $wgMaxArticleSize.
ArrayFunctions provides a number of configuration parameters to limit the computation time of certain functions and prevent users from performing a denial-of-service attack on your wiki. For backwards-compatibility reasons, these limits are off by default. The available configuration parameters are:
This configuration parameter limits the maximum number of #af_foreach iterations a page is allowed to perform. Setting it to anything below zero will impose no restrictions on the number of iterations.
This configuration parameter limits the maximum size of an array constructed with the #af_range parser function. Setting it to anything below zero will impose no restrictions on the size of arrays constructed with #af_range.
This configuration parameter limits the maximum length of a pipeline (#af_pipeline). Setting it to anything below zero will impose no restrictions on the number of steps in a pipeline.
These limits are reported in the parser limit report, which can be used to tweak the settings or debug a page.
FAQ
How can I define an array to be used throughout a page?
This way, the array is available in Template:My template as {{{1}}}.
How to iterate over an array?
It is possible to iteratively access elements of an array using #af_foreach:
{{#af_foreach:{{#af_list:red|green|blue}}||color|<nowiki/>
* {{{color}}} is my favourite.
}}
The expected output from the snipped above is:
red is my favourite.
green is my favourite.
blue is my favourite.
Why are values not recognized as arrays?
This may happen because your arrays are too large. MediaWiki internally keeps a counter on the total size of template arguments, which can be increased by increasing $wgMaxArticleSize.
How is this different from extensions such as Arrays or Variables?
The main difference between ArrayFunctions and those extensions is that ArrayFunctions parser functions are pure. This means that instead of modifying or declaring a variable, the parser function directly outputs its result.
For example with #af_map, the given array is not modified; instead, a copy is created, modified and then outputted. No parser function in ArrayFunctions modifies global state: all computation happens solely with the invocation of the parser function. This makes working with ArrayFunctions very different from working with other extensions such as Arrays or Variables. Instead of imperatively modifying an array stored in a variable, function composition must be used to perform more complex operations. For example:
This parser function casts a string to a boolean. This is useful for creating an array containing a boolean. The values that are considered as true are "1", "true", "on", and "yes". The values that are considered as false are "0", "false", "off", "no" and the empty string. If any other value is given, an error will be returned.
Description
{{#af_bool: value }}
Parameters
value : string or boolean
The value to cast to a boolean.
Return values
Returns the casted boolean.
Examples
Create an opaque representation of a boolean
{{#af_bool: yes }}, {{#af_bool: no }}, {{#af_bool: true }}
boolean__^__1, boolean__^__0, boolean__^__1
Create an array containing a boolean
{{#af_print: {{#af_list: {{#af_bool: yes}} }} }}
0: true
af_count
ArrayFunctions version
≥ 1.0
This parser functions counts the number of values in an array. By default, this parser function only counts the number of top-level elements. However, if the keyword argument recursive is given a true value, it will also count the number of items in any sub-arrays. Note that in case of recursive=true, both the element containing an array, as well as all values inside the array are counted.
Description
{{#af_count: array | recursive=recursive }}
Parameters
array : array
The array to count.
recursive : boolean, default=false
Whether to count items recursively.
Return values
The number of items in the array.
Examples
Count the number of items in a one-dimensional list
{{#af_print: {{#af_count: {{#af_list: a | b | c }} }} }}
3
Count the number of items in a multi-dimensional list
{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} }} }}
2
Recursively count the number of items in a multi-dimensional list
{{#af_print: {{#af_count: {{#af_list: {{#af_list: a | b }} | {{#af_list: c | d }} }} | recursive=true }} }}
6
af_difference
ArrayFunctions version
≥ 1.5
The difference between three arrays
This parser function computes the difference between arrays. It will compare the first array to zero or more other arrays, and returns the values in the first array that are not present in any of the other arrays. This function preserves the keys of the first array.
Description
{{#af_difference: array | *arrays }}
Parameters
array : array
The first array.
*arrays : array
The other arrays.
Return values
Returns an array containing all values from array that are not present in any of the arrays in arrays. The keys in array are preserved.
Examples
Compute the difference of three arrays
{{#af_print: {{#af_difference: {{#af_list: a | b | c }} | {{#af_list: a }} | {{#af_list: b}} }} }}
2: c
af_exists
ArrayFunctions version
≥ 1.0
This parser function checks whether the given key or index exists in the given array. Multiple keys can be given to check nested arrays.
Description
{{#af_exists: array | *keys }}
Parameters
array : array
The array to check.
keys : string or int
The key to check. Multiple keys can be given to check if a nested key exists.
Return values
Returns true if array contains the key, false otherwise.
{{#af_print: {{#af_exists: {{#af_list: a | b | c }} | 2 }} }}
true
Check if a nested key exists
{{#af_print: {{#af_exists: {{#af_list: a | {{#af_list: b | c }} }} | 0 | 3 }} }}
false
af_flatten
ArrayFunctions version
≥ 1.11
This parser function creates a new array with all sub-array elements concatenated into it recursively up to the specified depth, or until it is completely flattened if no depth is specified.
Description
{{#af_flatten: array | depth }}
Parameters
array : array
The array to flatten.
depth : integer, default=null
The depth to flatten to, or nothing to flatten until the array is completely flat.
Return values
Returns an array with all sub-array elements concatenated into it recursively up to the specified depth, or a completely flattened array if no depth is specified (equivalent to an infinite depth). For example, a depth of 1 means that all top-level elements are concatenated together, but any arrays below that are left as is.
Examples
Flatten an array one level deep completely
{{#af_print: {{#af_flatten: {{#af_list: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }} }}
0: a
1: b
2: c
3: d
4: e
5: f
Flatten an array two levels deep completely
{{#af_print: {{#af_flatten: {{#af_list: {{#af_list: {{#af_list: a | b | c }} }} }} }} }}
0: a
1: b
2: c
Flatten an array two levels deep once
{{#af_print: {{#af_flatten: {{#af_list: {{#af_list: {{#af_list: a | b | c }} }} }} | 1 }} }}
0
0: a
1: b
2: c
af_float
ArrayFunctions version
≥ 1.0
This parser function casts a string to a float. This is useful for creating an array containing a float. In case the string cannot be cast to a float, an error will be given. The precision of the floating point number is limited to that of PHP.
This parser function provides a way to iterate over arrays. For each of the elements in the given input array, it will evaluate its body, replacing any occurences of the key_name and value_name variables. These variables can be accessed in the same manner as template parameters, by surrounding them with triple braces (e.g. {{{value_name}}}).
{{#af_foreach: {{#af_list: John | Steve | Harry }} | | name | Hello, {{{name}}}! | delimiter=<br/> }}
Hello, John! Hello, Steve! Hello, Harry!
af_get
ArrayFunctions version
≥ 1.0
This parser function retrieves the element with the given index from the given array, or performs some operation if the index is overloaded. If the index does not exist and it is not an overloaded index, the empty string is returned.
An overloaded index allows the user to succinctly perform certain simple operations on arrays during indexing. Using an overloaded index is identical to first performing an #af_get with all indices before the overloaded index, then performing the function specified by the overloaded index, and then performing another #af_get with the remaining indices. For example, {{#af_get: {{{1}}} | a | * | b }} is equivalent to {{#af_get: {{#af_group: {{#af_get: {{{1}}} | a }} }} | b }}.
If the array contains a key that clashes with the symbol for an overloaded index, the value under the key will be retrieved instead. This was done for backwards compatibility reasons.
Description
{{#af_get: array | *indices }}
Parameters
array : array
The array in which to index.
*indices : string
The index. Multiple indices can be given to index nested arrays.
Overloaded indices (available since version 1.11.0)
This parser function selects one of two alternatives based on the given predicate. If the given predicate is evaluated to true, the second parameter is evaluated. Otherwise, the third parameter is evaluated (if given).
Description
{{#af_if: predicate | consequent | alternative }}
Parameters
predicate : boolean
The predicate.
consequent : string
The value to return if the predicate holds (i.e. is true).
alternative : string, default=""
The value to return if the predicate does not hold (i.e. is false).
Return values
Returns the consequent if the predicate holds, or the alternative if it is given and the predicate does not hold.
Examples
Check if a value is an array
{{#af_if: {{#af_isarray: not an array }} | A beautiful array! | Not an array! }}
Not an array!
af_int
ArrayFunctions version
≥ 1.0
This parser function casts a string to an integer. This is useful for creating an array containing an integer. If the string cannot be cast to an integer, an error will be given.
Description
{{#af_int: value }}
Parameters
value : string or int
The value to cast to an integer.
Return values
Returns the casted integer.
Examples
Create an opaque representation of an integer
{{#af_int: 42 }}, {{#af_int: -12 }}
integer__^__42, integer__^__-12
Create an array containing an integer
{{#af_print: {{#af_list: {{#af_int: -129}} }} }}
0: -129
af_intersect
ArrayFunctions version
≥ 1.2
The intersection of three arrays
This parser function computes the intersection of arrays. It computes an array containing all the elements of the first array that are also present in all of the other arrays. This function preserves the keys of the first array.
Description
{{#af_intersect: array | *arrays }}
Parameters
array : array
The first array.
*arrays : array
The other arrays.
Return values
Returns the intersection of the given arrays.
Examples
Compute the intersection of two identical arrays
{{#af_print: {{#af_intersect: {{#af_list: a | b | c }} | {{#af_list: a | b | c }} }} }}
0: a
1: b
2: c
Compute the intersection of two partially overlapping arrays
{{#af_print: {{#af_intersect: {{#af_list: a | b | c }} | {{#af_list: c | d | e }} }} }}
2: c
af_isarray
ArrayFunctions version
≥ 1.0
This parser function checks if the given value is an array.
Description
{{#af_isarray: value }}
Parameters
value : mixed
The value to check.
Return values
Returns true if value is an array, false otherwise.
Examples
Check if an array is an array
{{#af_print: {{#af_isarray: {{#af_list: a | b | c }} }} }}
true
Check if a string is an array
{{#af_print: {{#af_isarray: Hello, World! }} }}
false
af_join
ArrayFunctions version
≥ 1.0
This parser function recursively joins the items of an array together with a given separator. This function works by first flattening the array completely, and then joining the elements using the given separator.
Description
{{#af_join: array | glue }}
Parameters
array : array
The array to join.
glue : string, default=""
The string used to join each item.
Return values
Returns the joined array.
Examples
Join a one-dimensional array
{{#af_join: {{#af_list: a | b | c }} }}
abc
Join a one-dimensional array using a separator
{{#af_join: {{#af_list: a | b | c }} | \s-\s }}
a - b - c
Join a multi-dimensional array using a separator
{{#af_join: {{#af_list: a | b | {{#af_list: c | d }} }} | \s-\s }}
a - b - c - d
af_keysort
ArrayFunctions version
≥ 1.0
This parser function sorts a list of objects based on the values of the specified key. To sort an array by key, use #af_ksort.
This parser function applies a callback to each element of a list.
Description
{{#af_map: array | value_name | callback }}
Parameters
array : array
The array to run through the callback.
value_name : string
The name to give to the value in the callback.
callback : string
The callback to apply to each element of the array.
Return values
Returns the resulting mapped array.
Examples
Appending a string to each element
{{#af_print: {{#af_map: {{#af_list: a | b | c }} | v | {{{v}}}-appended }} }}
0: a-appended
1: b-appended
2: c-appended
Altering list elements
{{#af_print: {{#af_map: {{#af_list: {{#af_list: a }} | {{#af_list: b }} }} | v | {{#af_push: {{{v}}} | c }} }} }}
0
0: a
1: c
1
0: b
1: c
af_merge
ArrayFunctions version
≥ 1.2
This parser function computes the merges two or more arrays. It merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It does not remove duplicates.
If the input arrays have the same string key, then the later value for that key will overwrite the previous one. If the arrays contain numeric keys, later values will be appended instead and the later keys will be renumbered.
Description
{{#af_merge: array | *arrays }}
Parameters
array : array
The first array.
*arrays : array
The other arrays.
Return values
Returns the union of the given arrays.
Examples
Appending a string to each element
{{#af_print: {{#af_merge: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }}
0: a
1: b
2: c
3: d
4: e
5: f
af_object
ArrayFunctions version
≥ 1.0
This parser function creates a new object from the given parameters.
This parser function creates a pipeline of operations. The parser function takes the result from expanding the previous argument, and passes that as the specified parameter (or prev by default) to the next argument of the parser function. This is very useful to improve the readability of the template code, as it allows you to remove deeply nested parser function calls. For example, the following two snippets are semantically equivalent:
Before
After
{{#af_print: {{#af_unique: {{#af_reverse: {{#af_list: a | a | b | c}} }} }} }}
{{#af_pipeline: {{#af_list: a | a | b | c }}
| {{#af_reverse: {{{prev}}} }}
| {{#af_unique: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}
Returns the value returned by the final step of the pipeline.
Examples
Create a pipeline
{{#af_pipeline: {{#af_list: a | a | b | c }}
| {{#af_reverse: {{{prev}}} }}
| {{#af_unique: {{{prev}}} }}
| {{#af_print: {{{prev}}} }}
}}
0: c
1: b
2: a
af_print
ArrayFunctions version
≥ 1.0
This parser function prints the given value for debug purposes. Unlike #af_show, it does not parse the value. This function should only be used for debug purposes, consider using #af_show to display a value to the reader.
Description
{{#af_print: *values | end=end }}
Parameters
*values : mixed
The values to print.
end : string, default=""
The string to append to the end of each printed value.
Return values
Returns the value in human-readable form.
Examples
Print a boolean
{{#af_print: {{#af_bool: yes }} }}
true
Print a list
{{#af_print: {{#af_list: a | b | c }} }}
0: a
1: b
2: c
af_push
ArrayFunctions version
≥ 1.0
This parser function adds the given value to the end of the list.
Description
{{#af_push: array | value }}
Parameters
array : array
The array to append the value to.
value : mixed
The value to add.
Return values
Returns the array with the value appended.
Examples
Push a value
{{#af_print: {{#af_push: {{#af_list: a | b }} | c }} }}
0: a
1: b
2: c
af_put
ArrayFunctions version
≥ 1.13
This parser function sets the given value for the given index. It is identical to #af_set, except for the parameter order. This parser function should be preferred over #af_set.
Description
{{#af_put: array | value | *indices }}
Parameters
array : array
To array in which to set the index.
value : mixed
The value to set the index to.
*indices : string
The index to set. Multiple indices can be given to index nested arrays.
Return values
Returns the array with the given index set to the given value.
Examples
Replace an existing value
{{#af_print: {{#af_put: {{#af_list: a | b | c }} | d | 2 }} }}
{{#af_print: {{#af_set: {{#af_object: foo=bar }} | far | boo | far }} }}
foo: bar
boo
far: far
af_range
ArrayFunctions version
≥ 1.12
This parser function creates a finite range of integers. The ith element of a range is determined by the function r(i) = start + step * i. For a positive step, the bounds of the range are i >= 0 and r(i) < stop. For a negative step, the bounds are i >= 0 and r(i) > stop.
The behaviour of this parser function might be influenced by the $wgArrayFunctionsMaxRangeSize configuration parameter.
Description
{{#af_range: start | stop | step }}
Parameters
start : integer
The start of the range.
stop : integer, default=null
The end of the range (non-inclusive). If this value is omitted, the start will become the stop, and start will default to zero.
step : integer, default=1
The step size between range values.
Return values
A finite range of integers.
Examples
Create a range from 0 to 4
{{#af_print: {{#af_range: 0 | 5 }} }}
0: 0
1: 1
2: 2
3: 3
4: 4
Alternative syntax for a range from 0 to 4
{{#af_print: {{#af_range: 5 }} }}
0: 0
1: 1
2: 2
3: 3
4: 4
Create a range from 0 to -4
{{#af_print: {{#af_range: 0 | -5 | -1 }} }}
0: 0
1: -1
2: -2
3: -3
4: -4
Create a range of all odd numbers between 0 to 10
{{#af_print: {{#af_range: 1 | 10 | 2 }} }}
0: 0
1: 3
2: 5
3: 7
4: 9
af_reduce
ArrayFunctions version
≥ 1.2
This parser function iteratively reduces the array to a single value using a callback. It iteratively applies callable to the elements of the given array, so as to reduce the array to a single value. The callable is passed the value of the current iteration, as well as the result of the previous iteration.
This parser function reverses the given array. It does not preserve numeric keys.
Description
{{#af_reverse: array }}
Parameters
array : array
The array to reverse.
Return values
Returns the original array reversed.
Examples
Reverse a list
{{#af_print: {{#af_reverse: {{#af_list: a | b | c}} }} }}
0: c
1: b
2: a
af_search
ArrayFunctions version
≥ 1.3
This parser function searches the given array for the given value, and returns the first corresponding key if the value is found.
Description
{{#af_search: array | value }}
Parameters
array : array
The array to search in.
value : mixed
The value to search for.
Return values
Returns the first corresponding key if the value is found, nothing otherwise.
Examples
Search for a value in an array
{{#af_print: {{#af_search: {{#af_list: a | b | c }} | b }} }}
1
af_set
ArrayFunctions version
≥ 1.0
(deprecated in 1.13.0)
This parser function sets the given value for the given index. It has been deprecated in ArrayFunctions 1.13.0, and should no longer be used. It will remain available indefinitely for backwards compatibility. As an alternative, you should use #af_put instead.
Description
{{#af_set: value | array | *indices }}
Parameters
value : mixed
The value to set the index to.
array : array
To array in which to set the index.
*indices : string
The index to set. Multiple indices can be given to index nested arrays.
Return values
Returns the array with the given index set to the given value.
Examples
Replace an existing value
{{#af_print: {{#af_set: d | {{#af_list: a | b | c }} | 2 }} }}
{{#af_print: {{#af_set: far | {{#af_object: foo=bar }} | boo | far }} }}
foo: bar
boo
far: far
af_show
ArrayFunctions version
≥ 1.4
This parser function prints the given value in a human-readable format. The formatting of the value is based on format.
Description
{{#af_show: value | format=format }}
Parameters
value : mixed
The value to show.
format : string, default="simple" (available since version 1.16.0)
A comma-separated list of formats. The value will be shown using the first format supporting the type of the value.
Formats (available since version 1.16.0)
Name
Supported types
Description
simple
string, integer, float, boolean
Format the value as its string representation. This is the default format.
table
array
Format the value as a table.
Return values
Returns the value in human-readable form, formatted using format.
Examples
Show a value
{{#af_show: Hello World! }}
Hello World!
Show an integer
{{#af_show: {{#af_int: 42 }} | format=simple }}
42
af_slice
ArrayFunctions version
≥ 1.0
This parser function extracts a slice from the given array. Keys will be reset and reordered.
Description
{{#af_slice: array | offset | length }}
Parameters
array : array
The array to take a slice from.
offset : integer
The offset at which the slice starts. If non-negative, the slice will start at this given offset. If negative, the sequence will start that far from the end of the array.
length : integer, optional
The length of the slice. If the length is given and positive, then the slice will have that many elements in it. If the length is given and negative, then the slice will stop that many elements from the end of the array. If it is omitted, then the slice will have everything from offset up until the end of the array.
Return values
The slice.
Examples
Get the first two elements
{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | 0 | 2 }} }}
0: a
1: b
Get the last element
{{#af_print: {{#af_slice: {{#af_list: a | b | c }} | -1 }} }}
0: c
af_sort
ArrayFunctions version
≥ 1.0
This parser function sorts the given list. It does not preserve numeric keys.
{{#af_print: {{#af_sort: {{#af_list: b | c | a }} }} }}
0: a
1: b
2: c
Sort a list in descending order
{{#af_print: {{#af_sort: {{#af_list: b | c | a }} | descending=true }} }}
0: c
1: b
2: a
af_split
ArrayFunctions version
≥ 1.1
This parser function splits the given string based on a delimiter.
Description
{{#af_split: string | delimiter }}
Parameters
string : string
The string to split.
delimiter : string, default=","
The delimiter to use.
Return values
Returns the resulting list.
Examples
Split a string based on commas
{{#af_print: {{#af_split: a, b, c }} }}
0: a
1: b
2: c
Split a sentence into words
{{#af_print: {{#af_split: Lorem ipsum dolor et | \s }} }}
0: Lorem
1: ipsum
2: dolor
3: et
af_stringmap
ArrayFunctions version
≥ 1.6
This parser function applies a callback to each item of a delimited string, and returns the result as a delimited string, optionally with a different delimiter. This function is similar to Page Forms' #arraymap parser function.
The delimiter to split value on. If the empty string is given, "," is used.
value_name : string
The name to give to the value in the callback.
callback : string
The callback to apply to each element of the array.
new_delimiter : string, default=", "
The new delimiter to insert in between the mapped items.
conjunction : string, default=null
The delimiter to place between the last two items. This allows you to create a more natural summation, such as "Alice, Bob and Eve". If no value is given, the value of new_delimiter is used.
Return values
Returns the resulting delimited string.
Examples
Turn each item into a link
{{#af_stringmap: William Shakespeare, Stephen King, Mark Twain | , | x | [[{{{x}}}]] }}
Turn a comma-separated list into a human-readable list
{{#af_stringmap: William Shakespeare, Stephen King, Mark Twain | , | x | {{{x}}} | ,\s | and }}
William Shakespeare, Stephen King and Mark Twain
af_template
ArrayFunctions version
≥ 1.0
This parser function will invoke the given template and pass the values in the given array as arguments. In case the given array is an object, the values will be passed as named arguments. Otherwise, they will be passed as numeric arguments.
Description
{{#af_template: name | data }}
Parameters
name : string
The name of the template to invoke. If no namespace is given, it is assumed the page is in the template namespace, otherwise the given namespace is used. The page must exist, must be includable and must be readable by the user, otherwise an error is given.
data : array
The data to pass to the parameters. Values with numeric indices are passed as positional arguments and values with string indices are passed as named arguments.
This parser function will trim the given characters from the beginning and the end of the given string. The order of the characters does not matter. It will stop once it hits a character that is not in characters.
This parser function removes duplicate values from the given array. This function does not reset keys.
Description
{{#af_unique: array }}
Parameters
array : array
The array from which to remove duplicates.
Return values
Returns the array with duplicates removed.
Examples
Remove duplicates from an array
{{#af_print: {{#af_unique: {{#af_list: a | a | b | c | b }} }} }}
0: a
2: b
3: c
af_unset
ArrayFunctions version
≥ 1.0
This parser function removes the value associated with the given index from the array and returns the result. Numeric keys are not reset after calling this function.
Description
{{#af_unset: array | *indices }}
Parameters
array : array
The array from which to remove the given key.
*indices : string
The index to remove. Multiple indices can be given to index nested arrays.
Return values
Returns the array with the given index removed.
Examples
Remove a top-level index
{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 2 }} }}
0: a
1: b
Remove a top-level index, keys not reset
{{#af_print: {{#af_unset: {{#af_list: a | b | c }} | 1 }} }}
This parser functions takes one or more arrays and zips these together by key, creating an array where each key maps to an array containing all values under that key in each of the input arrays. The final array will only contain keys that were present in all of the input arrays. This means that if an input array contains a key not present in one of the other input arrays, the key will be dropped, and it will not be part of the final zipped array.
Description
{{#af_zip: array | *arrays }}
Parameters
array : array
The first array.
*arrays : array
The other arrays.
Return values
Returns the arrays zipped together.
Examples
Zip two lists of equal length
{{#af_print: {{#af_zip: {{#af_list: a | b | c }} | {{#af_list: d | e | f }} }} }}
0
0: a
1: d
1
0: b
1: e
2
0: c
1: f
Zip two lists of different lengths
{{#af_print: {{#af_zip: {{#af_list: a | b }} | {{#af_list: d | e | f }} }} }}
It is recommended to use LuaSandbox, because the order of arrays is not preserved when using LuaStandalone (see T349590).
This extension is particularly useful in combination with Lua, as it can be used to create the array containing data required for the presentation of the page. This array can be exported to work with ArrayFunctions:
This Lua function exports a Lua table as an ArrayFunctions array.
Description
mw.af.export( table )
Parameters
table : array
The array to export.
Return values
Returns the table as an ArrayFunctions array.
mw.af.import
ArrayFunctions version
≥ 1.6
This Lua function imports an ArrayFunctions array as a Lua table.
Description
mw.af.import( array )
Parameters
array : array
The array to import.
Return values
Returns the array as a Lua table.
Semantic MediaWiki
This extension integrates with Semantic MediaWiki by adding the arrayfunctions result format. The arrayfunctions result format is used to format query results as an ArrayFunctions array. The result is almost identical to the result of Semantic Scribunto's mw.smw.ask Lua function, except numeric indices start at zero.
arrayfunctions
ArrayFunctions version
≥ 1.14
This result format formats query results as an ArrayFunctions array.
Description
{{#ask: query | format=arrayfunctions }}
Return values
Returns the query result as an ArrayFunctions array.
This extension integrates with Cargo by adding the arrayfunctions display format. The arrayfunctions result format is used to format query results as an ArrayFunctions array.
arrayfunctions
ArrayFunctions version
≥ 1.15
This display format formats query results as an ArrayFunctions array.
Description
The no html parameter is required for ArrayFunctions to interpret the result correctly.
{{#cargo_query: parameters | no html | format=arrayfunctions }}
Return values
Returns the query result as an ArrayFunctions array.
Examples
Simple query
{{#af_print: {{#cargo_query: tables=Books
|no html
|fields=_pageName,Authors,Genres
|format=arrayfunctions
}} }}
0
_pageName: The Masque of the Red Death
Authors: Edgar Allan Poe
Genres
0: Horror
1: Fiction
1
_pageName: Rita Hayworth and Shawshank Redemption
Authors: Stephen King
Genres
0: Realism
1: Crime
2
_pageName: Animal Farm
Authors: George Orwell
Genres: Political satire
Magic words
The extension defines a number of magic words (variables).
AF_EMPTY
ArrayFunctions version
≥ 1.0
This magic word returns the empty array. This is useful, because it is not possible to create an empty array with #af_list or #af_object.
Description
{{AF_EMPTY}}
Return values
Returns the empty array.
Presentations
Presentation at SMWCon 2022 talking about ArrayFunctions
Presentation at EMWCon 2023 talking about ArrayFunctions
Workshop given at SMWCon 2023 about making your wiki Parsoid-compatible
Presentation at SMWCon 2023 talking about Parsoid and ArrayFunctions
Presentation at the MUD Workshop Spring 2025 about ArrayFunctions
See also
Extension:Arrays - similar extension that works by first defining and then manipulating arrays. Incompatible with Parsoid.
Ranges that exceed $wgArrayFunctionsMaxRangeSize are now taken into account when reporting the size of the largest constructed range in the parser limit report. Previously, only ranges that did not exceed the limit would be taken into account.
Fix TypeError that occurred when an error was propagated from a nested parser function call.
Add the $wgArrayFunctionsForeachIterationLimit configuration variable to limit the total number of iterations that can be performed by #af_foreach.
Add the $wgArrayFunctionsMaxRangeSize configuration variable to limit the maximum number of elements that an array constructed using #af_range can contain.
Add the #af_zip parser function.
Add the ! overloaded index to show a value.
Add the table and simple formats for `#af_show`.
Add the Pages with ArrayFunctions errors (af-error-category) tracking category.
Add error tracking and propagation.
Changed
Allow #af_show to take a format.
The #af_sort and #af_ksort parser functions now compare items normally instead of as strings.
Fix #af_template parser function to no longer check a user their read permissions explicitly, as regular template transclusion also does not do this. Previously, an error would be outputted.
Exporting a NULL value (e.g. through the mw.af.export Lua function) will now result in the empty string. Previously, the NULL would be returned unaltered.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.