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

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

Let’s run the following code to see how the last() function works.

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

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.

Free Resources