CMap implements a collection that takes key-value pairs.
You can access, add or remove an item with a key by using CMap::itemAt()
,
CMap::add()
, and CMap::remove()
. To get the number of the items in the map, use
CMap::getCount()
. CMap 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
Methods summary
public
|
#
__construct( array $data = null, boolean $readOnly = false )
Constructor. Initializes the list with an array or an iterable object.
Constructor. Initializes the list with an array or an iterable object.
Parameters
- $data
array $data the initial data. Default is null, meaning no initialization.
- $readOnly
boolean $readOnly whether the list is read-only
Throws
CException
If data is not null and neither an array nor an iterator.
|
public
boolean
|
#
getReadOnly( )
Returns
boolean whether this map is read-only or not. Defaults to false.
|
protected
|
#
setReadOnly( boolean $value )
Parameters
- $value
boolean $value whether this list is read-only or not
|
public
CMapIterator
|
#
getIterator( )
Returns an iterator for traversing the items in the list. This method is
required by the interface IteratorAggregate.
Returns an iterator for traversing the items in the list. This method is
required by the interface IteratorAggregate.
Returns
CMapIterator
an iterator for traversing the items in the list.
Implementation of
|
public
integer
|
#
count( )
Returns the number of items in the map. This method is required by Countable
interface.
Returns the number of items in the map. This method is required by Countable
interface.
Returns
integer number of items in the map.
Implementation of
|
public
integer
|
#
getCount( )
Returns the number of items in the map.
Returns the number of items in the map.
Returns
integer the number of items in the map
|
public
array
|
|
public
mixed
|
#
itemAt( mixed $key )
Returns the item with the specified key. This method is exactly the same as
CMap::offsetGet() .
Returns the item with the specified key. This method is exactly the same as
CMap::offsetGet() .
Parameters
Returns
mixed the element at the offset, null if no element is found at the offset
|
public
|
#
add( mixed $key, mixed $value )
Adds an item into the map. Note, if the specified key already exists, the old
value will be overwritten.
Adds an item into the map. Note, if the specified key already exists, the old
value will be overwritten.
Parameters
- $key
mixed $key key
- $value
mixed $value value
Throws
|
public
mixed
|
#
remove( mixed $key )
Removes an item from the map by its key.
Removes an item from the map by its key.
Parameters
- $key
mixed $key the key of the item to be removed
Returns
mixed the removed value, null if no such key exists.
Throws
|
public
|
#
clear( )
Removes all items in the map.
Removes all items in the map.
|
public
boolean
|
#
contains( mixed $key )
Parameters
Returns
boolean whether the map contains an item with the specified key
|
public
array
|
#
toArray( )
Returns
array the list of items in array
|
public
|
#
copyFrom( mixed $data )
Copies iterable data into the map. Note, existing data in the map will be
cleared first.
Copies iterable data into the map. Note, existing data in the map will be
cleared first.
Parameters
- $data
mixed $data the data to be copied from, must be an array or object implementing
Traversable
Throws
CException
If data is neither an array nor an iterator.
|
public
|
#
mergeWith( mixed $data, boolean $recursive = true )
Merges iterable data into the map.
Merges iterable data into the map.
Existing elements in the map will be overwritten if their keys are the same as
those in the source. If the merge is recursive, the following algorithm is
performed:
- the map data is saved as $a, and the source data is saved as $b;
- if $a and $b both have an array indexed at the same string key, the arrays
will be merged using this algorithm;
- any integer-indexed elements in $b will be appended to $a and reindexed
accordingly;
- any string-indexed elements in $b will overwrite elements in $a with the
same index;
Parameters
- $data
mixed $data the data to be merged with, must be an array or object implementing
Traversable
- $recursive
boolean $recursive whether the merging should be recursive.
Throws
CException
If data is neither an array nor an iterator.
|
public static
array
|
#
mergeArray( array $a, array $b )
Merges two or more arrays into one recursively. If each array has an element
with the same string key value, the latter will overwrite the former (different
from array_merge_recursive). Recursive merging will be conducted if both arrays
have an element of array type and are having the same key. For integer-keyed
elements, the elements from the latter array will be appended to the former
array.
Merges two or more arrays into one recursively. If each array has an element
with the same string key value, the latter will overwrite the former (different
from array_merge_recursive). Recursive merging will be conducted if both arrays
have an element of array type and are having the same key. For integer-keyed
elements, the elements from the latter array will be appended to the former
array.
Parameters
- $a
array $a array to be merged to
- $b
array $b array to be merged from. You can specify additional arrays via third
argument, fourth argument etc.
Returns
array the merged array (the original arrays are not changed.)
See
|
public
boolean
|
#
offsetExists( mixed $offset )
Returns whether there is an element at the specified offset. This method is
required by the interface ArrayAccess.
Returns whether there is an element at the specified offset. This method is
required by the interface ArrayAccess.
Parameters
- $offset
mixed $offset the offset to check on
Returns
boolean
Implementation of
|
public
mixed
|
#
offsetGet( integer $offset )
Returns the element at the specified offset. This method is required by the
interface ArrayAccess.
Returns the element at the specified offset. This method is required by the
interface ArrayAccess.
Parameters
- $offset
integer $offset the offset to retrieve element.
Returns
mixed the element at the offset, null if no element is found at the offset
Implementation of
|
public
|
#
offsetSet( integer $offset, mixed $item )
Sets the element at the specified offset. This method is required by the
interface ArrayAccess.
Sets the element at the specified offset. This method is required by the
interface ArrayAccess.
Parameters
- $offset
integer $offset the offset to set element
- $item
mixed $item the element value
Implementation of
|
public
|
#
offsetUnset( mixed $offset )
Unsets the element at the specified offset. This method is required by the
interface ArrayAccess.
Unsets the element at the specified offset. This method is required by the
interface ArrayAccess.
Parameters
- $offset
mixed $offset the offset to unset element
Implementation of
|