User Guide Cancel

ArraySwap

 

Description

Swaps array values of an array at specified positions. This function is more efficient than multiple cfset tags.

Returns

The function operates in-place and returns a boolean ("YES" or "NO") to indicate the success or failure of the operation.

Category

Array functions

Function syntax

ArraySwap(array, position1, position2)

See also

Functions for XML object management in Modifying a ColdFusion XML object in the Developing ColdFusion Applications

Parameters

Parameter

Description

array

Name of an array

position1

Position of first element to swap

position2

Position of second element to swap

Example

<cfscript>
superheroArray = ["Batman","Aquaman","Thor","Black Panther","Iron Man"];
writeOutput("Original array");
writeDump(superheroArray);
// Swap eelements 1 and 3
arraySwap(superheroArray,1,3);
writeOutput("Updated array");
writeDump(superheroArray);
</cfscript>
<cfscript> superheroArray = ["Batman","Aquaman","Thor","Black Panther","Iron Man"]; writeOutput("Original array"); writeDump(superheroArray); // Swap eelements 1 and 3 arraySwap(superheroArray,1,3); writeOutput("Updated array"); writeDump(superheroArray); </cfscript>
<cfscript>
    superheroArray = ["Batman","Aquaman","Thor","Black Panther","Iron Man"];
    writeOutput("Original array");
    writeDump(superheroArray);
    // Swap eelements 1 and 3
    arraySwap(superheroArray,1,3);
    writeOutput("Updated array");
    writeDump(superheroArray);
</cfscript>

Output

Get help faster and easier

New user?