Get the last element of an array in PHP
This post will discuss how to get the last element of an array (without deleting it) in PHP.
1. Using end() function
You can invoke the end() function to get the value at the end of the array. It returns the value present at the end of the array, and false if the array is empty.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php $array = array('One' => 1, 'Two' => 2, 'Three' => 3); $last = end($array); echo "Last Element: $last\n"; $current = current($array); echo "Current Element: $current\n"; /* Output: Last Element: 3 Current Element: 3 */ ?> |
Note that the end() function advances the internal pointer of the array to its last element. If you do not wish to modify the internal array pointer, pass the array as a parameter to a function and call the end() function. This works because the array is passed as a copy rather than a reference.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php function getLastItem($array) { return end($array); } $array = array('One' => 1, 'Two' => 2, 'Three' => 3); $last = getLastItem($array); echo "Last Element: $last\n"; $current = current($array); echo "Current Element: $current\n"; /* Output: Last Element: 3 Current Element: 1 */ ?> |
This function returns the “value” at the end of the array. To get the key at the end of the array, you can invoke the key() function after making a call to the end() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php function endKey($array) { end($array); return key($array); } $array = array('One' => 1, 'Two' => 2, 'Three' => 3); $last_value = end($array); echo "Last Value: $last_value\n"; $last_key = endKey($array); echo "Last Key: $last_key\n"; /* Output: Last Value: 3 Last Key: Three */ ?> |
2. Using array_key_last() function
As of PHP 7.3.0, array_key_last() function is the recommended way to get the last key of an array. It outperforms the end() function in terms of performance and avoids any side effects, such as moving the internal array pointer.
|
1 2 3 4 5 6 7 8 9 10 |
<?php $array = array('One' => 1, 'Two' => 2, 'Three' => 3); $last_key = array_key_last($array); echo $last_key; /* Output: Three */ ?> |
The array_key_last() function returns “key” at the end of the array. To get the value at the end of the array, you can use the array’s manual access notation.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $array = array('One' => 1, 'Two' => 2, 'Three' => 3); $last_key = array_key_last($array); echo "Last Key: $last_key\n"; if ($last_key !== null) { $last_value = $array[$last_key]; echo "Last Value: $last_value\n"; } /* Output: Last Key: Three Last Value: 3 */ ?> |
3. Using array_slice() function
For PHP <= 7.2, you can use the array_slice() function to fetch the last element of an array. It accepts the offset parameter, which denotes the position in the array, and extracts a slice from the array. If the offset is negative, the positioning will start from the array’s end.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $array = array("One" => "Apple", "Two" => "Orange", "Three" => "Banana"); $last = array_slice($array, -1); print_r($last); /* Output: Array ( [Three] => Banana ) */ ?> |
The array_slice() function acts as an efficient alternative to the end() function. However, it returns an array instead of last value. However, it returns an array instead of the last value. array_slice($array, -1)[0] or array_pop(array_slice($array, -1)) can be used to obtain the value.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $array = array("One" => "Apple", "Two" => "Orange", "Three" => "Banana"); if (count($array) > 0) { $last = array_values(array_slice($array, -1))[0]; echo $last; } /* Output: Banana */ ?> |
4. Using [] operator
If you have a numerical array, you can access its last element using array’s manual access notation. This only works with numerical arrays, but fails for associate arrays.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $array = [1, 2, 3, 4, 5]; $len = count($array); if ($len > 0) { $last = $array[$len - 1]; echo $last; } /* Output: 5 */ ?> |
That’s all about getting the last element of an array in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)