• PHP Video Tutorials

PHP - Map Functions



The Map is a sequential collection of key-value pairs, almost identical to an array used in a similar context. The keys can be of any type but must be unique, and the values are replaced if added to the map by using the same key.

Strengths

  • Keys and values can be any type, including objects.
  • Supports array syntax (square brackets).
  • Insertion order is preserved.
  • Performance and memory efficiency is very similar to an array.
  • Automatically frees allocated memory when its size drops low enough.

Weaknesses

  • Can’t be converted to an array when objects are used as keys.

Syntax

Ds\Map implements Ds\Collection {
   /* Constants */   
   
   const int MIN_CAPACITY = 16 ;
   /* Methods */
   
   public void allocate( int $capacity )
   public void apply( callable $callback )
   public int capacity( void )
   public void clear( void )
   public Ds\Map copy( void )
   public Ds\Map diff( Ds\Map $map )
   public Ds\Map filter([ callable $callback ] )
   public Ds\Pair first( void )
   public mixed get( mixed $key [, mixed $default ] )
   public bool hasKey( mixed $key )
   public bool hasValue( mixed $value )
   public Ds\Map intersect( Ds\Map $map )
   public bool isEmpty( void )
   public Ds\Set keys( void )
   public void ksort([ callable $comparator ] )
   public Ds\Map ksorted([ callable $comparator ] )
   public Ds\Pair last( void )
   public Ds\Map map( callable $callback )
   public Ds\Map merge( mixed $values )
   public Ds\Sequence pairs( void )
   public void put( mixed $key , mixed $value )
   public void putAll( mixed $pairs )
   public mixed reduce( callable $callback [, mixed $initial ] )
   public mixed remove( mixed $key [, mixed $default ] )
   public void reverse( void )
   public Ds\Map reversed( void )
   public Ds\Pair skip( int $position )
   public Ds\Map slice int $index [, int $length ] )
   public void sort([ callable $comparator ] )
   public Ds\Map sorted([ callable $comparator ] )
   public number sum( void )
   public array toArray( void )
   public Ds\Map union( Ds\Map $map )
   public Ds\Sequence values( void )
   public Ds\Map xor( Ds\Map $map )
}

Predefined Constants

Ds\Map::MIN_CAPACITY

Sr.No Function & Description
1

Ds\Map::allocate() Function

This Function can allocate enough memory for the required capacity.

2

Ds\Map::apply() Function

This Function can update update all values by applying a callback function to each value.

3

Ds\Map::capacity() Function

This Function can return the current capacity.

4

Ds\Map::clear() Function

This Function can remove remove all values.

5

Ds\Map::copy() Function

This Function can return the shallow copy of a map.

6

Ds\Map::count() Function

This Function can return the number of values in a map.

7

Ds\Map::diff() Function

This Function can create a new map by using keys that aren't in another map.

8

Ds\Map::filter() Function

This Function can create a new map by using a callable to determine which pairs to include.

9

Ds\Map::first() Function

This Function can return the first pair in a map.

10

Ds\Map::get() Function

This Function can return the value for a given key.

11

Ds\Map::hasKey() Function

This Function can determine whether the map contain a given key.

12

Ds\Map::hasValue() Function

This Function can determine whether the map contain a given value.

13

Ds\Map::intersect() Function

This Function can create a new map by intersecting keys with another map.

14

Ds\Map::isEmpty() Function

This Function can return return whether the map is empty.

15

Ds\Map::jsonSerialize() Function

This Function can return a representation that can be converted to JSON.

16

Ds\Map::keys() Function

This Function can return the set of map's keys.

17

Ds\Map::ksort() Function

This Function can sort the map in-place by key.

18

Ds\Map::ksorted() Function

This Function can return a copy, sorted by key.

19

Ds\Map::last() Function

This Function can return the last pair of a map.

20

Ds\Map::map() Function

This Function can return the result of applying a callback to each value.

21

Ds\Map::merge() Function

This Function can return the result of adding all given associations.

22

Ds\Map::pairs() Function

This Function can return a sequence containing all pairs of the map.

23

Ds\Map::put() Function

This Function can associate a key with a value.

24

Ds\Map::putAll() Function

This Function can associate all key-value pairs of traversable object or array.

25

Ds\Map::reduce() Function

This Function can reduce the map to a single value by using a callback function.

26

Ds\Map::remove() Function

This Function can remove and return a value by key.

27

Ds\Map::reverse() Function

This Function can can reverse the map in-place.

28

Ds\Map::reversed() Function

This Function can return a reversed copy.

29

Ds\Map::skip() Function

This Function can return the pair at a given positional index.

30

Ds\Map::slice() Function

This Function can return a subset of the map defined by starting index and length.

31

Ds\Map::sort() Function

This Function can sort the map in-place by value.

32

Ds\Map::sorted() Function

This Function can return a copy sorted by value.

33

Ds\Map::sum() Function

This Function can return the sum of all values in a map.

34

Ds\Map::toArray() Function

This Function can convert a map to an array.

35

Ds\Map::union() Function

This Function can create a new map using values from the current instance and another map.

36

Ds\Map::values() Function

This Function can return a sequence of the map's values.

37

Ds\Map::xor() Function

This Function can create a new map using the keys of either current instance or of another map, but not of both.

php_function_reference.htm
Advertisements