Home >Backend Development >PHP Tutorial >How to Get the Last Element of a PHP Array Without Modifying It?
Getting the Last Array Element without Modification
While array_pop() offers a convenient way to retrieve the last element of an array, it permanently removes it. To bypass this limitation and obtain the last element without deleting it, PHP provides alternative approaches:
Using end():
For PHP 7.3.0 and Later:
Example Usage:
For the array $array = array('a' => 'a', 'b' => 'b', 'c' => 'c'):
Note that $array remains intact in both scenarios, preserving its original structure.
The above is the detailed content of How to Get the Last Element of a PHP Array Without Modifying It?. For more information, please follow other related articles on the PHP Chinese website!