arrayNew

Creates an array of 1-3 dimensions. Index array elements with square brackets: [ ]. CFML arrays expand dynamically as data is added.

arrayNew(dimension [, unsynchronized]) → returns Array

arrayNew Argument Reference

dimension
Required
Default: 1

Values:
  • 1
  • 2
  • 3

unsynchronized
Default: NO

CF 2016+ When true creates an unsynchronized array. Unsynchronized arrays are not thread safe so they should not be used within shared scopes (application, session, etc). According to the CF2016 Performance whitepaper: Unsynchronized arrays are about 93% faster due to lock avoidance.

Links more information about arrayNew

Examples sample code invoking the arrayNew function


Create the One dimensional array

Uses the arrayNew function to create the new array

newArray = arrayNew(1);
someArray = arraySet(newArray, 1, 4, "All is well");
writeOutput( serializeJSON(newArray) );

Expected Result: ["All is well", "All is well", "All is well", "All is well"]


Create the Two dimensional array

Uses the arrayNew function to create the new array

newArray = arrayNew(2);
newArray[1][1] = "First value";
newArray[1][1] = "First value";
newArray[1][2] = "First value";
newArray[2][1] = "Second value";
newArray[2][2] = "Second value";
writeOutput( serializeJSON(newArray) );

Expected Result: [["First value", "First value"],["Second value", "Second value"]]


Create unsynchronized array

CF 2016+ Uses the arrayNew function to create the new unsynchronized array

newArray = arrayNew(1, true);
newArray.append("one");
writeOutput( serializeJSON(newArray) );

Expected Result: ["one"]


Create an array using implicit notation

CF 8+ Instead of using arrayNew you can also create an array using square brackets.

newArray = ["one","two"];
writeOutput( serializeJSON(newArray) );

Expected Result: ["one", "two"]


Fork me on GitHub