For PHP < 8.5.0 && >= 7.3.0:
if (! function_exists("array_last")) {
function array_last(array $array) {
return $array ? $array[array_key_last($array)] : null;
}
}(PHP 8 >= 8.5.0)
array_last — Restituisce l'ultimo valore di un array
Restituisce l'ultimo valore dell'array specificato.
arrayUn array.
Restituisce l'ultimo valore di array se l'array non è vuoto;
null altrimenti.
Example #1 Utilizzo di base di array_last()
<?php
$array = [1 => 'a', 0 => 'b', 3 => 'c', 2 => 'd'];
$lastValue = array_last($array);
var_dump($lastValue);
?>Il precedente esempio visualizzerà:
string(1) "d"
For PHP < 8.5.0 && >= 7.3.0:
if (! function_exists("array_last")) {
function array_last(array $array) {
return $array ? $array[array_key_last($array)] : null;
}
}There is another Polyfill for PHP < 8.5
if (! function_exists('array_last')) {
function array_last(array $array): mixed {
return $array ? array_slice($array, -1, 1) : null;
}
}