Whatever message this page gives is out now! Go check it out!

ArrayFindLast

Last update:
Jun 5, 2026
Searches an array from the end and returns the 1-based index of the last matching value, or the last element that satisfies a closure.

Description

While ArrayFind() returns the index of the first occurrence of a value in an array, ArrayFindLast() searches from the end and returns the index of the last occurrence. It also supports closure-based searching, allowing developers to find the last element matching a custom condition.

Syntax

ArrayFindLast(Array array, Any value_or_closure)

Member Function Syntax

array.findLast(Any value_or_closure)
ParameterTypeDescription
arrayArray (required)The array to search.
value_or_closureAny (required)A value for exact matching, or a closure/function that receives each element and returns true or false.

Returns

Integer: Returns the 1-based index of the last occurrence of the value, or the last element matching the closure, or 0 if not found.

Usage Notes

  • ArrayFindLast() supports both global function syntax and member function syntax.
  • The closure receives each element and should return true for a match.
  • The function works with 1D, 2D, and 3D arrays when searching within sub-arrays.
  • For exact matching, the value is compared against array elements in reverse order until a match is found.

Basic: Find Last Occurrence of a Value


<cfscript>
fruits = ["apple", "banana", "orange", "apple", "grape", "banana"];
lastApple = ArrayFindLast(fruits, "apple");
writeOutput("Last 'apple' at position: " & lastApple);
// Output: Last 'apple' at position: 4
</cfscript>

Compare ArrayFind vs ArrayFindLast


<cfscript>
arr = ["first", "second", "first", "third", "first"];
firstResult = ArrayFind(arr, "first");
lastResult = ArrayFindLast(arr, "first");
writeOutput("ArrayFind: " & firstResult & ", ArrayFindLast: " & lastResult);
// Output: ArrayFind: 1, ArrayFindLast: 5
</cfscript>

Member Function Syntax


<cfscript>
fruits = ["apple", "banana", "orange", "apple", "grape", "banana"];
lastBanana = fruits.findLast("banana");
writeOutput("Last 'banana' at position: " & lastBanana);
// Output: Last 'banana' at position: 6
</cfscript>

Closure-Based Search


<cfscript>
fruits = ["apple", "banana", "orange", "apple", "grape", "banana"];
lastStartsWithA = ArrayFindLast(fruits, function(item) {
    return item.startsWith("a");
});
writeOutput("Last fruit starting with 'a' at position: " & lastStartsWithA);
// Output: Last fruit starting with 'a' at position: 4
</cfscript>

Not Found: Returns 0


<cfscript>
fruits = ["apple", "banana", "orange"];
lastMango = ArrayFindLast(fruits, "mango");
writeOutput("Last 'mango' at position: " & lastMango & " (not found)");
// Output: Last 'mango' at position: 0 (not found)
</cfscript>

Numeric Values


<cfscript>
numbers = [10, 20, 30, 20, 40, 20];
first20 = ArrayFind(numbers, 20);
last20 = ArrayFindLast(numbers, 20);
writeOutput("ArrayFind 20: " & first20 & " (first)");
writeOutput("ArrayFindLast 20: " & last20 & " (last)");
// Output: ArrayFind 20: 2 (first)
//         ArrayFindLast 20: 6 (last)
</cfscript>

2D Array: Search Within Sub-Arrays


<cfscript>
a2 = ArrayNew(2);
a2[1][1] = "apple";
a2[1][2] = "banana";
a2[1][3] = "apple";
a2[1][4] = "cherry";
a2[1][5] = "apple";
a2[2][1] = "date";
a2[2][2] = "apple";
a2[2][3] = "fig";

lastApple1 = a2[1].findLast("apple");
lastApple2 = a2[2].findLast("apple");
writeOutput("Last 'apple' in a2[1]: " & lastApple1);   // 5
writeOutput("Last 'apple' in a2[2]: " & lastApple2);   // 2
</cfscript>

2D Array: Find Sub-Array in Outer Array


<cfscript>
a1 = ArrayNew(2);
a1[1][1] = "one-one";
a1[1][2] = "one-two";
a1[1][3] = "one-three";
a1[2][1] = "two-one";
a1[2][2] = "two-two";
a1[2][3] = "two-three";

array1d = a1[1];
result = a1.findLast(array1d);
writeOutput("Finding 1d array (first dimension): " & result);
// Output: Finding 1d array (first dimension): 1
</cfscript>

2D Array: ArrayFind vs ArrayFindLast Comparison


<cfscript>
a3 = ArrayNew(2);
a3[1][1] = "X";
a3[1][2] = "Y";
a3[1][3] = "X";
a3[1][4] = "Z";
a3[1][5] = "X";

writeOutput("ArrayFind 'X' in a3[1]: " & ArrayFind(a3[1], "X") & " (first)");
writeOutput("ArrayFindLast 'X' in a3[1]: " & ArrayFindLast(a3[1], "X") & " (last)");
writeOutput("Member find 'X': " & a3[1].find("X") & " (first)");
writeOutput("Member findLast 'X': " & a3[1].findLast("X") & " (last)");
// Output: ArrayFind: 1, ArrayFindLast: 5, find: 1, findLast: 5
</cfscript>

3D Array: Search Deep Sub-Arrays


<cfscript>
a2 = ArrayNew(3);
a2[1][1][1] = "apple";
a2[1][1][2] = "banana";
a2[1][1][3] = "apple";
a2[1][1][4] = "cherry";
a2[1][1][5] = "apple";

firstApple = ArrayFind(a2[1][1], "apple");
lastApple = ArrayFindLast(a2[1][1], "apple");
writeOutput("ArrayFind 'apple': " & firstApple & " (first)");
writeOutput("ArrayFindLast 'apple': " & lastApple & " (last)");
// Output: ArrayFind: 1, ArrayFindLast: 5
</cfscript>

3D Array: Nested Navigation with findLast


<cfscript>
a3 = ArrayNew(3);
a3[1][1][1] = 100;
a3[1][1][2] = 200;
a3[1][1][3] = 300;
a3[1][1][4] = 200;
a3[1][1][5] = 400;
a3[1][1][6] = 200;

array1d = a3[1][1];
result1 = a3[1].findLast(array1d);
result2 = ArrayFindLast(a3[1][1], 200);
result3 = a3[1][1].findLast(200);

writeOutput("Finding 1d array in 2d: " & result1);         // 1
writeOutput("ArrayFindLast 200: " & result2);               // 6
writeOutput("Member findLast 200: " & result3);             // 6
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page