arrayInsertAt

Description

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.

Categories

Related

History

ColdFusion MX: Changed behavior: This function can be used on XML objects. Changed thrown exceptions: This function can throw the InvalidArrayIndexException error.

Syntax

ArrayInsertAt(array, position, value)

Attributes

AttributeDescriptionRequiredDefault
arrayName of an array
positionIndex position at which to insert value
valueValue to insert

Returns

True, on successful completion.

Usage

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 myarray[2][4]:
<cfset ArrayInsertAt(myarray[2], 4, "test")>

Example

<h3>ArrayInsertAt Example</h3> 
<!--- Create a new array. ---> 
<cfset DaysArray = ArrayNew(1)> 
<!--- Populate an element or two. ---> 
<cfset DaysArray[1] = "Monday"> 
<cfset DaysArray[2] = "Tuesday"> 
<cfset DaysArray[3] = "Thursday"> 
<!--- Add an element before position 3. ---> 
Add an element before position 3: 
    <cfoutput>#ArrayInsertAt(DaysArray,3,"Wednesday")#</cfoutput> 
Now output the array as a list: 
<cfoutput>#ArrayToList(DaysArray)#</cfoutput> 
<!--- The array now has four elements. Element 3, "Thursday", has become element four. --->