I can use the following to get the value of the last item of $array
. How can I get a reference to that item?
$last_item = end($array);
The items of $array
are indexed arrays.
1) Using the array length property The length property returns the number of elements in an array. Subtracting 1 from the length of an array gives the index of the last element of an array using which the last element can be accessed.
So to reference an element of a two-dimensional array, the first parameter is the row that you are referencing and the second parameter is the column you are referencing. So array2[1][3] would be referencing the element of the index of row 1, column 3 (the 2nd row and the fourth column).
The Last element is nothing but the element at the index position that is the length of the array minus-1. If the length is 4 then the last element is arr[3].
findLastIndex() The findLastIndex() method returns the index of the last element in an array that satisfies the provided testing function.
end($array); $referenceToLastElement = &$array[key($array)];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With