array.flatten Function |
The array.flatten function expands single array elements (of type array) into multiple elements.
array.flatten(ident);
ident | Required. The array to flatten |
The source array with all elements of type array expanded into multiple elements - 'flattened'.
The following example creates an array with three elements. The first two elements are '1' and '2', the third element is another array containing the elements '3' and '4'. This is shown in the first line of output.
The array is then flattened and shown in the second line of output.
$arr34 = array.new(3, 4); PRINT $arr34; /* [3,4] */ $arr12 = array.new(1, 2, $arr34); PRINT $arr12; /* [1,2,[3,4]] */ PRINT array.flatten($arr); /* [1,2,3,4] */
Note how the third element of arr12 (which is arr34) has been 'flattened' to two separate elements in the arr12 array. The above code snippet produces the following output:
[3,4] [1,2,[3,4]] [1,2,3,4] |
array.first - get the first element of an array
array.join - convert an array to a string by joining all the elements
array.last - get the last element of an array
array.new - create a new array
array.range - return a sub-array from a specified array
array.set_at - modify the data at a specified index of an array
array.get_at - retrieve the data at a specified index of an array
length - get the number of elements in an array