array_key_last

(PHP 7 >= 7.3.0)

array_key_lastGets the last key of an array

Description

array_key_last ( array $array ) : mixed

Get the last key of the given array without affecting the internal array pointer.

Parameters

array

An array.

Return Values

Returns the last key of array if the array is not empty; NULL otherwise.

See Also

  • array_key_first() - Gets the first key of an array
  • end() - Set the internal pointer of an array to its last element
add a note add a note

User Contributed Notes 7 notes

up
18
Anyvie at devlibre dot fr
1 year ago
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];
    }
}
up
2
ccb_bc at hotmail dot com
3 months ago
<?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
?>
up
4
@manzoorwanijk
11 months ago
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;
    }
}
?>
up
2
contact at duzun dot me
3 months ago
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;
    }
}
?>
up
1
github / k-gun
3 months ago
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;
}
?>
up
1
rotexdegba007-github at yahoo dot ca
3 months ago
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));
        }
    }
up
-2
wes at nospam dot example dot org
5 months ago
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));
    }
}

?>
To Top