arrayNew

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

arrayNew(dimension [, isSynchronized]) → returns array

Argument Reference

dimension numeric
Required
Default: 1

Values:
  • 1
  • 2
  • 3

isSynchronized boolean
Default: true

CF 2016+ Lucee 5.1+ When true creates a synchronized 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

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"]

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"]]

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

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

Expected Result: ["one"]

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"]

CF 2018+ When using data types on array creation, items are converted if possible, otherwise an error is thrown.

typedArray = arrayNew['boolean'](1);
typedArray[1] = true;
typedArray[2] = 'true';
typedArray[3] = 1;
typedArray[4] = '1';
typedArray[5] = 'yes';

typelessArray = arrayNew(1);
typelessArray[1] = true;
typelessArray[2] = 'true';
typelessArray[3] = 1;
typelessArray[4] = '1';
typelessArray[5] = 'yes';
writeOutput(serializeJSON([typedArray,typelessArray]));

Expected Result: [[true,true,true,true,null,true],[true,"true",1,"1",null,"yes"]]

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub