Array Functions¶
An array is a data structure that stores a collection of elements of the same data type, such as integers, characters, or strings. The elements are arranged in a contiguous block of memory, with each element occupying a specific position within the array. The position of each element is identified by its index, which is a non-negative integer value that starts from 0 for the first element and increases by 1 for each subsequent element.
Arrays are commonly used to store a list of records that can be easily accessed and manipulated using loops and other programming constructs. They offer an efficient way to access and process data, as well as to organise and manage it.
Mythradon support the following array functions:
- array\at()
- array\includes()
- array\indexOf()
- array\join()
- array\length()
- array\push()
- array\removeAt()
- array\unique()
Note
Use the list() function to create a new array.
Creating an Array¶
An array can be defined using the following syntax options:
array\at( )¶
The array\at() function retrieves the value at the specified INDEX from the LIST, where the first element is indexed at 0.
Example:
$list = array\push($list, 'one','two','three','four');
$value = array\at($list, 2); // $value will be 'three'
output\printLine($value);
Note
Passing an array argument by reference is not supported. Therefore, you will need to reassign the array to the result returned by the function.
array\includes( )¶
The array\includes() function checks if the LIST contains the specified VALUE and returns true if it does. This function is applicable to both Array and Multi-Enum fields.
Examples:
$list = array\push($list, 'one','two','three','four');
output\printLine( array\includes($list, 'three') ); // Prints true
$list = array\push($list, 'one','two','three','four');
output\printLine( array\includes($list, 'five') ); // Prints false
array\indexOf( )¶
The array\indexOf() function can be used to return the position of a specified ELEMENT within a LIST in Mythradon. If the ELEMENT is not found within the LIST, the function will return null. This function is compatible with both Array and Multi-Enum fields.
Example:
$numberList = array\push($idList, 'one','two','three','four');
$value = array\indexOf($numberList, 'three'); // $value will be 2
output\printLine($value);
Note
In Mythradon, array indexing starts at zero.
array\join( )¶
The array\join() function concatenates the elements of an array using a specified string SEPARATOR and returns the resulting string.
Example:
$numberList = array\push($idList, 'one','two','three','four');
output\printLine( array\join($numberList, ' - ') ); // Prints 'one - two - three - four'
array\length( )¶
The array\length() function returns the count of elements in the LIST.
Example
$numberList = array\push($idList, 'one','two','three','four');
output\printLine( array\length($numberList) ); // Prints 4
array\push( )¶
The array\push() function appends one or more elements to the end of an array and returns the updated array.
Note
An array argument is not passed by reference. You need to re-assign the array to a function result.
Example:
$numberList = array\push($idList, 'one','two','three','four');
output\printLine( array\push($numberList, 'five', 'six', 'seven') ); // Prints ["one","two","three","four","five","six","seven"]
array\removeAt( )¶
The array\removeAt() function removes an element from the array based on its INDEX and returns a new array with the updated elements.
Examples:
$numberList = array\push($idList, 'one','two','three','four');
$value = array\indexOf($numberList, 'three');
output\printLine($value); // $value will be 2
$newArray = array\removeAt($numberList, $value);
output\printLine($newArray); // $newArray = ["one","two","four"]
array\unique( )¶
The array\unique() function takes a LIST as input, removes any duplicate elements, and returns a new array with only the unique elements.
Examples:
$list = List(9,6,7,2,9,9,8,3,4,1,8,7,4,2,1);
$newArray = array\unique($list);
output\printLine($newArray); // $newArray = [9,6,7,2,8,3,4,1]
See Also¶
- Array Functions
- Datetime Functions
- Entity Functions
- Env Functions
- Ext Functions
- JSON Functions
- Language Functions
- Number Functions
- Object Functions
- Output Functions
- Password Functions
- Record Functions
- String Functions
- Mythradon Marketing
- Mythradon Sales
- Mythradon Service
- Mythradon System Administration
- Mythradon Tools