Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is the Ds\Map::last() function in PHP?

Khizar Hayat Saani

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 Map class in PHP is a collection of key-value pairs. The structure of a Map is similar to an array in terms of the sequenceinsertion order is preserved, performance, and memory efficiency.

The last() function is used to return the last key-value pair in a map.

The Ds\Map::last() function

Syntax

The following is the function prototype:

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

Parameters and return value

The last() function does not take any input parameters.

The function returns the last key-value pair in the map.

Code

<?php

// Creating a Map
$map = new \Ds\Map(["0" => "a", 
            "1" => "b", "2" => "c", "3" => "d", "4" => "e", "5" => "f"]);
  
print_r($map->last());
  
?>

Output

Ds\Pair Object
(
[key] => 5
[value] => f
)

In the example above, we create a simple map.

Using the last() function, we are able to directly print the last key-value pair in the map.

Note: To run this program, you must first install Data Structures for PHP on your local machine.

RELATED TAGS

php
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