Arrays
 
 
 

An array is an ordered list of values. All values in an array must be of the same type. You can make arrays of integers, floats, strings, or vectors. Arrays grow as you add elements to them.

To declare an array variable, use:

int $ari[];

You can set the initial size of the array by putting a number inside the square brackets:

float $arf[4];
string $temp[3];

Getting and setting the value of an array element

To assign a value to a certain element in an array variable, put the element number (known as the index to the array) in square brackets after the variable name in the assignment statement:

$arf[2] = 45.646;
$temp[50] = "Glonk!";

To get the value of an array element, just use the variable name with the index in square brackets:

print($arf[2]); // 45.646
$temp[51] = $temp[49];

Remember that the numbering of the elements in an array starts at 0. The index of the first element is 0, the second element is 1, and so on. This means that the maximum index of an array is always one less than the number of elements in the array.

string $array[3] = {"first\n", "second\n", "third\n"};
print($array[0]); // Prints "first\n"
print($array[1]); // Prints "second\n"
print($array[2]); // Prints "third\n"

Literal representation

The literal representation of an array is a list of values separated by commas (all of the same type, of course), surrounded by curly braces:

{1, 2, 3, 4}
{"blue", "red", "black"}

You can assign literal values to an array variable with or without explicit declaration:

$rip = {1, 2, 3, 4};
string $hats = {"blue", "red", "black"};
string $shoes[3] = {"black", "brown", "blue suede"};

Arrays are only one dimensional

Arrays can only hold scalar values. You cannot create an array of arrays. However, you can use the matrix datatype to create a two-dimensional table of floating point values.