Sunday, November 23, 2025
HomeLanguagesPHP array_​key_​last() Function

PHP array_​key_​last() Function

The array_​key_​last() function is an inbuilt function in PHP that is used to get the last key of an array. This function returns the last key without affecting the internal array pointer.

Syntax:

int|string|null array_​key_​last(array $array)

Parameters: This function accepts single parameter $array that holds the array element.

Return Value: This function returns the last key of the array if the array is not empty, and null otherwise.

Example 1:

PHP




<?php
  
$arr = array();
  
var_dump(array_key_last($arr));
  
$arr1 = array("10", "20", "30", "40", "50");
var_dump(array_key_last($arr1));
  
?>


Output:

NULL
int(4)

Example 2:

PHP




<?php
  
$arr = array(
    'Geeks'  => "HTML"
    'GFG'  => "CSS"
    'Geek'  => "JavaScript"
    'G4G' => "PHP"
);
  
var_dump(array_key_last($arr));
  
$arr1 = array(
    5 => "neveropen",
    10 => "GFG"
);
  
var_dump(array_key_last($arr1));
  
?>


Output:

string(3) "G4G"
int(10)

Reference: https://www.php.net/manual/en/function.array-key-last.php

RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS