array_last

(PHP 8 >= 8.5.0)

array_last获取数组的最后一个值

说明

array_last(array $array): mixed

获取指定 array 的最后一个值。

参数

array

数组。

返回值

如果数组非空,则返回 array 的最后一个值;否则返回 null

示例

示例 #1 基础 array_last() 用法

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

$lastValue = array_last($array);

var_dump($lastValue);
?>

以上示例会输出:

string(1) "d"

参见

添加备注

用户贡献的备注 2 notes

up
0
cx dot moro+php at gmail dot com
12 minutes 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