PHP Ds\Map Functions Complete Reference

A Map is a sequential collection of key-value pair which is very similar to the array. The key of a map can be of any type and it is unique. If any value added to the same key in a Map then the map value will replace. It is the efficient Data Structure in PHP 7 to provide the alternative of an array.
Requirements: PHP 7 is required for both extension and the compatibility polyfill.
Installation: The easiest way to install data structure by using PECL extension.
pecl install ds
Syntax:
public Ds\Map::functionName()
Example: Below programs illustrate the Ds\Map::filter() function in PHP:
PHP
<?php// PHP program to illustrate the filter()// function of Ds\map// Creating a Map$map = new \Ds\Map([ 1 => "Welcome", 2 => "to", 3 => "Geeks", 4 => "for", 5 => "Geeks"]); // Display new sequence using filter functionvar_dump($map->filter(function($key, $val) { return $key % 3 == 0;}));?> |
Output:
object(Ds\Map)#3 (1) {
[0]=>
object(Ds\Pair)#2 (2) {
["key"]=>
int(3)
["value"]=>
string(5) "Geeks"
}
}
Complete list of data structure DS\Map:
|
PHP Ds\Map Functions |
Description |
|---|---|
| allocate() | Allocate enough memory for required capacity. |
| apply() | Apply a specific operation to all of the elements present in the map |
| capacity() | Return the current capacity of the map. |
| clear() | It is used to clear a Map object. |
| __construct() | It is used to creates a new instance. |
| copy() | Get a shallow copy of the specified Map instance. |
| count() | Count the number of elements present in the Map. |
| diff() | Create a map using the key which contains the elements of the first map which are not present in another map. |
| filter() | Create a new map using the filter function. |
| first() | Get the first key-value pair from the Map instance. |
| get() | Return the value of the given key. |
| hasKey() | Check whether a given Key is present in the Map object or not. |
| hasValue() | Determines whether the map contains a given value |
| intersect() | Create a new map that contains the intersection with another map. |
| isEmpty() | Check whether a given Map is empty or not. |
| keys() | Set of keys of the current Map instance. |
| ksort() | Sort the map element in-place by key. |
| ksorted() | Returns a copy which is sorted by key. |
| last() | Find and get the last key-value pair from a Map object. |
| map() | Apply a callback function to a Map object. |
| merge() | Return the result of adding all given associations. |
| pairs() | Get all of the pairs from the specified Map instance. |
| put() | It is used to associates a key with a value. |
| putAll() | Associate all key-value pairs of a traversable object or array. |
| reduce() | Reduce the map to a single value by applying operations using the callback function. |
| reverse() | In-place reverse the elements of a specified Map instance. |
| reversed() | Get a copy of the reverse of elements of a specified Map instance. |
| skip() | Return the pair at a given positional index. |
| slice() | Get a subset of the specified Map instance. |
| sort() | In-place sort the elements of a specified Map instance according to the values. |
| sorted() | Get a copy of the specified Map instance sorted according to the values. |
| sum() | Get the sum of all of the values present in the Map instance. |
| toArray() | Get an array generated by converting the Map instance into an Array. |
| union() | Create a new map that contains the union of two maps. |
| values() | Return a sequence of the map’s values. |
| xor() | Create a new map that contains the value either in the first map or the second map but not both. |



