array_last

(PHP 8 >= 8.5.0)

array_lastGets the last value of an array

Description

array_last(array $array): mixed

Get the last value of the given array.

Parameters

array

An array.

Return Values

Returns the last value of array if the array is not empty; null otherwise.

Examples

Example #1 Basic array_last() Usage

<?php
$array
= [1 => 'a', 0 => 'b', 3 => 'c', 2 => 'd'];

$lastValue = array_last($array);

var_dump($lastValue);
?>

The above example will output:

string(1) "d"

See Also

add a note

User Contributed Notes 2 notes

up
0
cx dot moro+php at gmail dot com
4 hours ago
jurugi+123456789 at gmail dot com is wrong because associative array doesn't have a Key 0 or key length + 1
up
0
jurugi+123456789 at gmail dot com
1 day ago
This seems like such a 'junior with nothing to do' sort of thing to add to PHP; I can think of no purpose for this. To get first array is always array[0], to get last value is array[length-1]. For compatibility it will be pretty bad to add right away, as like 99% won't just update or have 8.5 available. I can only see this be useful in a way like if MySQL has to return 1 value then you may check with 1 line if it's retrieved; but actually, based on the logic of those code, you would retrieve it only after checking the result or numeffected rows if that's a possibility, and bad code will just ignore that and create warnings even in cases when it's possible. So I see nothing useful here at least for existing code as majorly to maintain usability you'd want any practical and good code to just get 1st array value for all versions which is simple for PHP any version.
To Top