Inserts a value into an array. Array elements whose indexes are equal to or greater than the new position are incremented by one. The array length increases by one.
Returns the updated array with the specified element inserted.
ArrayInsertAt(array, position, value)
ArrayDeleteAt; Functions for XML object management in Modifying a ColdFusion XML object in the Developing ColdFusion Applications
ColdFusion (2018 release): Returns the updated array.
ColdFusion MX:
Parameter |
Description |
|---|---|
array |
Name of an array |
position |
Index position at which to insert value |
value |
Value to insert |
To apply the ArrayInsertAt() function to a multidimensional array, you must specify all but the last index in the array parameter. The following example inserts an element at myarray24:
<cfset ArrayInsertAt(myarray[2], 4, "test")>
If this function attempts to insert an element at position 0, or specifies a value for position that is greater than the size of array, this function throws an InvalidArrayIndexException error.
<cfscript>
myCities=["London","New York","Paris","Tokyo","Barcelona"];
WriteOutput("The input array is: ");
WriteOutput(serializeJSON(myCities) & "|");
// Insert the city "Berlin" in myCities after "Paris" at index four
ArrayInsertAt(myCities,4,"Berlin"); // Returns True
WriteOutput("Array after inserting Berlin is: ");
WriteOutput(serializeJSON(myCities)); // returns output array with Berlin as new element
</cfscript>
The input array is: ["London","New York","Paris","Tokyo","Barcelona"]|Array after inserting Berlin is: ["London","New York","Paris","Berlin","Tokyo","Barcelona"]
Sign in to your account