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"

参考

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 1 note

up
0
jurugi+123456789 at gmail dot com
7 hours 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.
+add a note

AltStyle によって変換されたページ (->オリジナル) /