ArrayDeleteAt

Description

Deletes an element from a specified position in an array.

When an element is deleted, ColdFusion recalculates index positions. For example, in an array that contains the months of the year, deleting the element at position 5 removes the entry for May. After this, to delete the entry for June, you would delete the element at position 5 (not 6).

Returns

True, on successful completion.

Function syntax

ArrayDeleteAt(array, position)

See also

ArrayInsertAt; Functions for XML object management in the Developing ColdFusion Applications

History

ColdFusion MX:

  • Changed behavior: This function can be used on XML objects.

  • Changed thrown exceptions: This function can throw the InvalidArrayIndexException error.

Parameters

Parameter

Description

array

Name of an array

position

Array position

Throws

If this function attempts to delete 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.

Example

<h3>ArrayDeleteAt Example</h3><p> 
<!--- Create an array. ---> 
<cfset DaysArray = ArrayNew(2)> 
<!--- Populate an element or two. ---> 
<cfset DaysArray[1][1] = "Monday"> 
<cfset DaysArray[2][1] = "Tuesday"> 
<cfset DaysArray[3][1] = "Wednesday"> 
<cfset DaysArray[1][2] = "April 12"> 
<cfset DaysArray[2][2] = "April 13"> 
<cfset DaysArray[3][2] = "April 14"> 
<p>This is what the array looks like before delete:<br> 
<cfoutput> 
#DaysArray[1][1]#&nbsp;&nbsp;#DaysArray[1][2]#<br> 
#DaysArray[2][1]#&nbsp;&nbsp;#DaysArray[2][2]#<br> 
#DaysArray[3][1]#&nbsp;&nbsp;#DaysArray[3][2]#<br> 
</cfoutput> 
 
<cfoutput> 
We delete this element of the array:<br> 
#ArrayDeleteAt(DaysArray,2)#<br> 
</cfoutput> 
<!--- The formerly third element, "Wednesday" is now the second element. ---> 
<p>This is what the array looks like after delete:<br> 
<cfoutput> 
#DaysArray[1][1]#&nbsp;&nbsp;#DaysArray[1][2]#<br> 
#DaysArray[2][1]#&nbsp;&nbsp;#DaysArray[2][2]#<br> 
</cfoutput>