TMap class
TMap implements a collection that takes key-value pairs.
You can access, add or remove an item with a key by using itemAt, add, and remove. To get the number of the items in the map, use getCount. TMap can also be used like a regular array as follows,
- $map[$key]=$value; // add a key-value pair
- unset($map[$key]); // remove the value with the specified key
- if(isset($map[$key])) // if the map contains the key
- foreach($map as $key=>$value) // traverse the items in the map
- $n=count($map); // returns the number of items in the map
| Method Summary |
|
void
|
add
( mixed $key, mixed $value)
Adds an item into the map.
|
|
void
|
Removes all items in the map.
|
|
boolean
|
|
|
void
|
Copies iterable data into the map.
|
|
integer
|
Returns the number of items in the map.
|
|
integer
|
|
|
Iterator
|
Returns an iterator for traversing the items in the list.
|
|
array
|
|
|
boolean
|
|
|
mixed
|
Returns the item with the specified key.
|
|
void
|
Merges iterable data into the map.
|
|
boolean
|
Returns whether there is an element at the specified offset.
|
|
mixed
|
Returns the element at the specified offset.
|
|
void
|
Sets the element at the specified offset.
|
|
void
|
Unsets the element at the specified offset.
|
|
mixed
|
Removes an item from the map by its key.
|
|
protected
void
|
|
|
array
|
|
| Method Details |
add
| public void add |
(mixed $key , mixed $value ) |
Adds an item into the map.
Note, if the specified key already exists, the old value will be overwritten.
| Input |
| mixed | $key | key |
| mixed | $value | value |
| Output |
| Exception |
| throws | TInvalidOperationException if the map is read-only |
|
clear
Removes all items in the map.
|
contains
| public boolean contains |
(mixed $key ) |
| Input |
| mixed | $key | the key |
| Output |
|
boolean
| whether the map contains an item with the specified key |
| Exception |
|
copyFrom
| public void copyFrom |
(mixed $data ) |
Copies iterable data into the map.
Note, existing data in the map will be cleared first.
| Input |
| mixed | $data | the data to be copied from, must be an array or object implementing Traversable |
| Output |
| Exception |
| throws | TInvalidDataTypeException If data is neither an array nor an iterator. |
|
count
Returns the number of items in the map.
This method is required by Countable interface.
| Output |
|
integer
| number of items in the map. |
| Exception |
|
getCount
| public integer getCount |
() |
| Output |
|
integer
| the number of items in the map |
| Exception |
|
getIterator
| public Iterator getIterator |
() |
Returns an iterator for traversing the items in the list.
This method is required by the interface IteratorAggregate.
| Output |
|
Iterator
| an iterator for traversing the items in the list. |
| Exception |
|
getKeys
| Output |
|
array
| the key list |
| Exception |
|
getReadOnly
| public boolean getReadOnly |
() |
| Output |
|
boolean
| whether this map is read-only or not. Defaults to false. |
| Exception |
|
itemAt
| public mixed itemAt |
(mixed $key ) |
Returns the item with the specified key.
This method is exactly the same as offsetGet.
| Input |
| mixed | $key | the key |
| Output |
|
mixed
| the element at the offset, null if no element is found at the offset |
| Exception |
|
mergeWith
| public void mergeWith |
(mixed $data ) |
Merges iterable data into the map.
Existing data in the map will be kept and overwritten if the keys are the same.
| Input |
| mixed | $data | the data to be merged with, must be an array or object implementing Traversable |
| Output |
| Exception |
| throws | TInvalidDataTypeException If data is neither an array nor an iterator. |
|
offsetExists
| public boolean offsetExists |
(mixed $offset ) |
Returns whether there is an element at the specified offset.
This method is required by the interface ArrayAccess.
| Input |
| mixed | $offset | the offset to check on |
| Output |
| Exception |
|
offsetGet
| public mixed offsetGet |
(integer $offset ) |
Returns the element at the specified offset.
This method is required by the interface ArrayAccess.
| Input |
| integer | $offset | the offset to retrieve element. |
| Output |
|
mixed
| the element at the offset, null if no element is found at the offset |
| Exception |
|
offsetSet
| public void offsetSet |
(integer $offset , mixed $item ) |
Sets the element at the specified offset.
This method is required by the interface ArrayAccess.
| Input |
| integer | $offset | the offset to set element |
| mixed | $item | the element value |
| Output |
| Exception |
|
offsetUnset
| public void offsetUnset |
(mixed $offset ) |
Unsets the element at the specified offset.
This method is required by the interface ArrayAccess.
| Input |
| mixed | $offset | the offset to unset element |
| Output |
| Exception |
|
remove
| public mixed remove |
(mixed $key ) |
Removes an item from the map by its key.
| Input |
| mixed | $key | the key of the item to be removed |
| Output |
|
mixed
| the removed value, null if no such key exists. |
| Exception |
| throws | TInvalidOperationException if the map is read-only |
|
setReadOnly
| protected void setReadOnly |
(boolean $value ) |
| Input |
| boolean | $value | whether this list is read-only or not |
| Output |
| Exception |
|
toArray
| Output |
|
array
| the list of items in array |
| Exception |
|