Get and change the size of an array
 
 
 

Use the size function to get the size of an array:

string $hats[3] = {"blue", "red", "black"};
print(size($hats)); // 3

The size of an array increases automatically as needed. Let’s say you have an array with two elements. If you try to assign to a third element of the array, the array size automatically increases to three elements. If you query the value of an element beyond the array size, a value of 0 is returned. For a string array, empty quotation marks would be returned.

int $scores[]; // Declared as a zero element array.
$scores[150] = 3; // Now a 151 element array.
$scores[200] = 5; // Now a 201 element array.

The second statement above gives the array 151 elements and assigns element index 150 the value 3. The third statement expands the array to 201 elements and assigns element index 200 the value 5.

When you assign a value in an array, Maya reserves memory for all elements up to that number. If you are not careful, you can exceed the capacity of your computer with a single array declaration. For example, the following two statements would force you to quit Maya on most computers:

int $bigBoy[];
$bigBoy[123456789] = 2; // DANGER!

Clear an array

Use the clear function to free the memory used by an array and leave it with zero elements.

string $hats[] = {"blue", "red", "black"};
clear($hats);
print(size($hats)); // 0