Sunday, October 5, 2025
HomeLanguagesPHP | DsMap last() Function

PHP | Ds\Map last() Function

The Ds\Map::last() function in PHP is used to find and get the last key-value pair from a Map object.

If the map is empty then this function throws an exception.

Syntax:

Ds\Pair public Ds\Map::last ( void )

ParameterDs\Pair: It does not accepts any parameter.

Return value: This function returns the last key-value pair from the Map object. If the Map object is empty then this function raises UnderflowException.

Below program illustrate the Ds\Map::last() function in PHP:

Program 1:




<?php
// PHP program to illustrate the last()
// function of Ds\map
  
// Creating a Map
$map = new \Ds\Map(["1" => "Geeks"
            "2" => "for", "3" => "Geeks"]);
  
// Print last key-value pair
print_r($map->last());
  
?>


Output:

Ds\Pair Object
(
    [key] => 3
    [value] => Geeks
)

Program 2:




<?php
// PHP program to illustrate the last()
// function of Ds\map
  
// Creating a Map
$map = new \Ds\Map(["1" => "10"
            "2" => "20", "3" => 30]);
  
// Print last key-value pair
print_r($map->last());
  
?>


Output:

Ds\Pair Object
(
    [key] => 3
    [value] => 30
)

Reference: http://php.net/manual/en/ds-map.last.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6823 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS