ARRAY_REPEAT
Description
The array_repeat function is used to create a new array consisting of a specified number of identical elements. This function accepts two parameters: expr which represents the element to be repeated, and count which represents the number of repetitions. The function returns an array of type array<T>, where T is the type of the element.
Parameter Description
expr(T): The element to be repeated.count(int): The number of times the element needs to be repeated.
Return Type
- Returns an array of type
array<T>.
Usage Example
- Repeat a string:
result:
- Duplicate Values:
result:
- Use in conjunction with other functions: {#use-in-conjunction-with-other-functions}
result:
- Repeated Date and Time:
result:
Notes
- Ensure the
countparameter is a non-negative integer. Ifcountis negative, the function will return an empty array. - When
expris a complex data type (such as an object or array), the entire structure will be repeated, not individual elements.
By using the array_repeat function, you can easily create arrays with repeated elements, simplifying the logic for data processing and transformation.
