Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

array_key_last
php
array
communitycreator

What is the array_key_last method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The array_key_last can be used to get the last key of the array.

Syntax

array_key_last(array $array): int|string|null

If the passed array doesn’t have any element, then it returns null.

Example

<?php
$arr = [
            'ONE' => 1, 
            'TWO' => 2, 
            'THREE' => 3
          ];

$lastKey = array_key_last($arr);

echo "The last key of the array is :". $lastKey;

?>

In the code above, We have created an array and get the last key of the array using the array_key_last method.

Example 2

Let’s call array_key_last on an empty array:

<?php

  $arr = [];
  $lastKey = array_key_last($arr);

  var_dump($lastKey);

?>

In the code above, we will get NULL as a result because the passed array is empty.

RELATED TAGS

array_key_last
php
array
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring