array_key_last

(PHP 7 >= 7.3.0, PHP 8)

array_key_last获取一个数组的最后一个键值

说明

array_key_last ( array $array ) : mixed

取得指定数组的 array 最后一个键值,不会影响到原数组的内部指针。

参数

array

要操作的数组。

返回值

返回 array 的最后一个键值(如果不为空),否则返回 null

参见

  • array_key_first() - 获取指定数组的第一个键值
  • end() - 将数组的内部指针指向最后一个单元

User Contributed Notes

dracveg at gmail dot com 14-Jul-2021 05:07
Notice!

$a = array(
    1 => 1,
    0 => 0
);

echo (array_key_last($a)); // output: "0", not "1"
donaldinou at gmail dot com 02-Sep-2020 12:58
Here come my benchmark, and

return key(array_slice($array, -1));

is the my best answer for a polyfill.

<?php

function array_key_last_1(array $array) {
   
// assert(!empty($array));
   
return key(array_slice($array, -1));
}

function
array_key_last_2(array $array) {
    return
key(array_slice($array, -1, 1, true));
}

function
array_key_last_3(array $array) {
    return
key(array_reverse($array));
}

function
array_key_last_4(array $array) {
    return
array_keys($array)[count($array) - 1] ?? null;
}

function
array_key_last_5(array $array) {
    return
array_keys($array)[count($array) - 1] ?? null;
}

function
array_key_last_6(array $array) {
   
$copy = array_keys($array);
    return
end($copy);
}

$result = array();
$array = range(0, 1000000);

for(
$i=1; $i < 7;$i++) {
 
$callback = 'array_key_last_' . $i;
 
$before = microtime(true);
 
call_user_func_array($callback, array($array));
 
$after = microtime(true);
 
$time = ($after-$before);
 
$result[$callback] = $time;
}

asort($result);
foreach (
$result as $key => $value) {
    echo
'[' . $key . ']' . $value . " sec" . PHP_EOL;
}
rnealxp at yahoo dot com 12-Apr-2020 03:18
Now that we have this function, where we once used...
<?php
$idxLast
= count($arr) - 1;
?>
we can now optimize code speed by using...
<?php
$idxLast
= (array_key_last($arr) ?? 0);
?>
Use-cases include lots of looping and control-structures.
If you do not know ahead of time that the array is numerically indexed, and that it could possibly be associative...safer/faster to stick with count().
williamdes at wdes dot fr 29-Mar-2020 09:43
https://www.php.net/manual/fr/function.array-key-last.php#123950

did return the first key of my array only:
"array_keys($array)[count($array) - 1] ?? null" works

I hope the example is fine for everyone else.

My array is:
[
            new NameExpression('d', 0),
            new NameExpression('f', 0),
            new NameExpression('g', 0),
]
webmaster at redinfo dot co dot kr 16-Oct-2019 06:09
// if : php ver < 7
// array_key_last

$arr = array('1'=>'bear1','2'=>array('22'=>'bear22'),'3'=>'bear3');
echo end(array_keys($arr));

// result : 3
contact at duzun dot me 03-Jul-2019 07:17
Try to beat this polyfill in terms of performance!

<?php
if( !function_exists('array_key_last') ) {
    function
array_key_last(array $array) {
        if( !empty(
$array) ) return key(array_slice($array, -1, 1, true));
    }
}

// Bonus
if (!function_exists('array_key_first')) {
    function
array_key_first(array $arr) {
        foreach(
$arr as $key => $unused) return $key;
    }
}
?>
ccb_bc at hotmail dot com 28-Jun-2019 04:15
<?php
   
// PHP >= 7
   
function _array_key_last(array $array){
        return (!empty(
$array)) ? array_keys($array)[count($array)-1] : null;
    }
   
var_dump(_array_key_last(['PHP', 'Javascript', 'Python'])); // 2
 
?>
github / k-gun 18-Jun-2019 04:41
For those who loves tested candies;

<?php
function array_key_last(array $array) {
   
// 100000 iters: ~0.068 secs
   
return key(array_slice($array, -1));
   
// 100000 iters: ~0.070 secs
   
return key(array_reverse($array));
   
// 100000 iters: ~0.088 secs
   
return array_keys($array)[count($array) - 1] ?? null;
}
?>
rotexdegba007-github at yahoo dot ca 16-Jun-2019 04:54
This polyfill works for PHP 5.6+.
It is a slight modification of "wes at nospam dot example dot org"'s example:

<?php

   
if( !function_exists('array_key_first') ) {

        function
array_key_first(array $array) {

            if(
$array === [] ) { return NULL; }

            foreach(
$array as $key => $_) { return $key; }
        }
    }

    if( !
function_exists('array_key_last') ) {

        function
array_key_last(array $array) {

            if(
$array === [] ) { return null; }
            
           
// the last 2 args to array_slice are crucial
           
return array_key_first(array_slice($array, -1, null, true));
        }
    }
wes at nospam dot example dot org 09-May-2019 09:37
Correct polyfill (one that doesn't make copies of arrays, and that doesn't make use of the IAP) is:

<?php

if(!function_exists('array_key_first')){
    function
array_key_first(array $array){
        if(
$array === []){
            return
NULL;
        }
        foreach(
$array as $key => $_) return $key;
    }
}

if(!
function_exists('array_key_last')){
    function
array_key_last(array $array){
        if(
$array === []){
            return
NULL;
        }
        return
array_key_first(array_slice($array, -1));
    }
}

?>
@manzoorwanijk 27-Oct-2018 09:41
For PHP < 7.3.0 :

Will work for any type of array

<?php
if ( ! function_exists( 'array_key_last' ) ) {
   
/**
     * Polyfill for array_key_last() function added in PHP 7.3.
     *
     * Get the last key of the given array without affecting
     * the internal array pointer.
     *
     * @param array $array An array
     *
     * @return mixed The last key of array if the array is not empty; NULL otherwise.
     */
   
function array_key_last( $array ) {
       
$key = NULL;

        if (
is_array( $array ) ) {

           
end( $array );
           
$key = key( $array );
        }

        return
$key;
    }
}
?>
Anyvie at devlibre dot fr 08-Aug-2018 10:21
For PHP <= 7.3.0 :

if (! function_exists("array_key_last")) {
    function array_key_last($array) {
        if (!is_array($array) || empty($array)) {
            return NULL;
        }
       
        return array_keys($array)[count($array)-1];
    }
}