array_first() and array_last() in PHP 8.5
Marcos Marcolin • May 7, 2025 • 1 min read
php php85 phprfc opensource🐘 PHP 8.5 will have two more array functions: array_first() and array_last().
Finally, we'll have dedicated functions to get the first and last elements of an array, without needing to use functions that lack a consistent pattern or that mutate the array internally.
These functions complement the already existing array_key_first() and array_key_last().
<?php
$colors = ['red', 'green', 'blue'];
/* Before PHP 8.5 */
// First element
echo reset($colors); // red
echo $colors[array_key_first($colors)]; // red
// Last element
echo end($colors); // blue
echo $colors[array_key_last($colors)]; // blue
/* 🐘 With PHP 8.5 */
echo array_first($colors); // red
echo array_last($colors); // blue
You can read more about the RFC in the official post: click here.