Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a reference to the last item of an array

Tags:

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.

like image 766
Emanuil Rusev Avatar asked Jan 26 '12 21:01

Emanuil Rusev


People also ask

How can you access the last element of the array?

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.

How do you reference part of an array?

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).

What is the last element in an array?

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

How do you find the index of the last element?

findLastIndex() The findLastIndex() method returns the index of the last element in an array that satisfies the provided testing function.


1 Answers

end($array); $referenceToLastElement = &$array[key($array)]; 
like image 105
Mark Baker Avatar answered Oct 07 '22 16:10

Mark Baker