CMap
鍖 | system.collections |
---|---|
缁ф壙 | class CMap » CComponent |
瀹炵幇 | IteratorAggregate, Traversable, ArrayAccess, Countable |
瀛愮被 | CAttributeCollection, CConfiguration, CCookieCollection, CFormElementCollection, CTypedMap |
婧愯嚜 | 1.0 |
鐗堟湰 | $Id: CMap.php 3518 2011-12-28 23:31:29Z alexander.makarow $ |
婧愮爜 | framework/collections/CMap.php |
浣犲彲浠ラ氳繃浣跨敤 itemAt锛add锛屼互鍙remove璁块棶锛屾坊鍔狅紝鍒犻櫎椤圭洰銆 閫氳繃璋冪敤getCount鍙幏寰楀湪map鐨勯」鐩暟閲忋 CMap鍚屾牱涔熷彲浠ュ儚涓涓櫘閫氱殑鏁扮粍浣跨敤锛屽
$map[$key]=$value; //娣诲姞涓涓敭鍚嶏紞閿煎 unset($map[$key]); //鍒犻櫎鎸囧畾閿悕鐨勯敭鍊 if(isset($map[$key])) //濡傛灉map鍖呭惈閿 foreach($map as $key=>$value) //閬嶅巻map鐨勯」鐩 $n=count($map); //杩斿洖map涓殑椤圭洰鏁
鍏叡灞炴
灞炴 | 绫诲瀷 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|---|
count | integer | 杩斿洖map涓殑椤圭洰鏁般 | CMap |
iterator | CMapIterator | 杩斿洖閬嶅巻杩欎釜鍒楄〃鐨勯」鐩殑杩唬鍣ㄣ | CMap |
keys | array | 杩斿洖閿悕鍒楄〃 | CMap |
readOnly | boolean | 杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆 | CMap |
鍏叡鏂规硶
鏂规硶 | 鎻忚堪 | 瀹氫箟鍦 |
---|---|---|
__call() | 濡傛灉绫讳腑娌℃湁璋冪殑鏂规硶鍚嶏紝鍒欒皟鐢ㄨ繖涓柟娉曘 | CComponent |
__construct() | 鏋勯犳柟娉曘 | CMap |
__get() | 杩斿洖涓涓睘鎬у笺佷竴涓簨浠跺鐞嗙▼搴忓垪琛ㄦ垨涓涓涓哄悕绉般 | CComponent |
__isset() | 妫鏌ヤ竴涓睘鎬ф槸鍚︿负null銆 | CComponent |
__set() | 璁剧疆涓涓粍浠剁殑灞炴у笺 | CComponent |
__unset() | 璁剧疆涓涓粍浠剁殑灞炴т负null銆 | CComponent |
add() | 娣诲姞涓涓」鐩埌map銆 | CMap |
asa() | 杩斿洖杩欎釜鍚嶅瓧鐨勮涓哄璞° | CComponent |
attachBehavior() | 闄勫姞涓涓涓哄埌缁勪欢銆 | CComponent |
attachBehaviors() | 闄勫姞涓涓涓哄垪琛ㄥ埌缁勪欢銆 | CComponent |
attachEventHandler() | 涓轰簨浠堕檮鍔犱竴涓簨浠跺鐞嗙▼搴忋 | CComponent |
canGetProperty() | 纭畾灞炴ф槸鍚﹀彲璇汇 | CComponent |
canSetProperty() | 纭畾灞炴ф槸鍚﹀彲鍐欍 | CComponent |
clear() | 鍒犻櫎map涓墍鏈夐」鐩 | CMap |
contains() | CMap | |
copyFrom() | 灏嗚凯浠e櫒涓殑鏁版嵁澶嶅埗鍒癿ap銆 | CMap |
count() | 杩斿洖map涓殑椤圭洰鏁般 | CMap |
detachBehavior() | 浠庣粍浠朵腑鍒嗙涓涓涓恒 | CComponent |
detachBehaviors() | 浠庣粍浠朵腑鍒嗙鎵鏈夎涓恒 | CComponent |
detachEventHandler() | 鍒嗙涓涓瓨鍦ㄧ殑浜嬩欢澶勭悊绋嬪簭銆 | CComponent |
disableBehavior() | 绂佺敤涓涓檮鍔犺涓恒 | CComponent |
disableBehaviors() | 绂佺敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
enableBehavior() | 鍚敤涓涓檮鍔犺涓恒 | CComponent |
enableBehaviors() | 鍚敤缁勪欢闄勫姞鐨勬墍鏈夎涓恒 | CComponent |
evaluateExpression() | 璁$畻涓涓狿HP琛ㄨ揪寮忥紝鎴栨牴鎹粍浠朵笂涓嬫枃鎵ц鍥炶皟銆 | CComponent |
getCount() | 杩斿洖map涓殑椤圭洰鏁般 | CMap |
getEventHandlers() | 杩斿洖涓涓簨浠剁殑闄勫姞澶勭悊绋嬪簭鍒楄〃銆 | CComponent |
getIterator() | 杩斿洖閬嶅巻杩欎釜鍒楄〃鐨勯」鐩殑杩唬鍣ㄣ | CMap |
getKeys() | 杩斿洖杩斿洖閿悕鍒楄〃 | CMap |
getReadOnly() | 杩斿洖杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆 | CMap |
hasEvent() | 纭畾涓涓簨浠舵槸鍚﹀畾涔夈 | CComponent |
hasEventHandler() | 妫鏌ヤ簨浠舵槸鍚︽湁闄勫姞鐨勫鐞嗙▼搴忋 | CComponent |
hasProperty() | 纭畾灞炴ф槸鍚﹁瀹氫箟銆 | CComponent |
itemAt() | 杩斿洖鎸囧畾浣嶇疆鐨勯」鐩 | CMap |
mergeArray() | 閫掑綊鍚堝苟涓や釜鎴栧涓暟缁勩 | CMap |
mergeWith() | 灏嗚凯浠e櫒鐨勬暟鎹暣鍚堝埌map銆 | CMap |
offsetExists() | 杩斿洖鍊艰鏄庢寚瀹氫綅缃槸鍚﹀瓨鍦ㄥ厓绱犮 | CMap |
offsetGet() | 杩斿洖鍊兼寚瀹氫綅缃殑鍏冪礌銆 | CMap |
offsetSet() | 璁剧疆鎸囧畾浣嶇疆鐨勫厓绱犮 | CMap |
offsetUnset() | 鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱犮 | CMap |
raiseEvent() | 鍙戣捣涓涓簨浠躲 | CComponent |
remove() | 鏍规嵁閿悕浠巑ap涓垹闄や竴涓」鐩 | CMap |
toArray() | CMap |
灞炴ц缁
杩斿洖map涓殑椤圭洰鏁般
杩斿洖閬嶅巻杩欎釜鍒楄〃鐨勯」鐩殑杩唬鍣ㄣ 姝ゆ柟娉曚负鎺ュ彛IteratorAggregate寮哄埗瑕佹眰瀹炵幇銆
杩斿洖閿悕鍒楄〃
杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆
鏂规硶璇︾粏
public void __construct(array $data=NULL, boolean $readOnly=false)
| ||
$data | array | 鍒濆鍖栫殑鏁版嵁銆傞粯璁や负null锛屾剰鍛崇潃娌℃湁鍒濆鍖栥 |
$readOnly | boolean | 鏍囪瘑杩欎釜鍒楄〃鏄惁涓哄彧璇汇 |
public function __construct($data=null,$readOnly=false)
{
if($data!==null)
$this->copyFrom($data);
$this->setReadOnly($readOnly);
}
鏋勯犳柟娉曘 鏍规嵁鏁扮粍鎴栬呰凯浠e璞″垵濮嬪寲杩欎釜鍒楄〃銆
public void add(mixed $key, mixed $value)
| ||
$key | mixed | 閿悕 |
$value | mixed | 閿 |
public function add($key,$value)
{
if(!$this->_r)
{
if($key===null)
$this->_d[]=$value;
else
$this->_d[$key]=$value;
}
else
throw new CException(Yii::t('yii','The map is read only.'));
}
娣诲姞涓涓」鐩埌map銆 璇锋敞鎰忥紝濡傛灉鎸囧畾鐨勯敭宸茬粡瀛樺湪锛屾棫鍊煎皢琚鐩栥
public void clear()
|
public function clear()
{
foreach(array_keys($this->_d) as $key)
$this->remove($key);
}
鍒犻櫎map涓墍鏈夐」鐩
public boolean contains(mixed $key)
| ||
$key | mixed | 閿悕 |
{return} | boolean | 杩斿洖鍊艰鏄巑ap涓槸鍚﹀寘鍚寚瀹氶敭鍚嶅搴旂殑閿笺 |
public function contains($key)
{
return isset($this->_d[$key]) || array_key_exists($key,$this->_d);
}
public void copyFrom(mixed $data)
| ||
$data | mixed | 瑕佸鍒剁殑鏁版嵁, 鍙兘鏄暟缁勬垨鑰呯户鎵夸簬Traversable鐨勫璞° |
public function copyFrom($data)
{
if(is_array($data) || $data instanceof Traversable)
{
if($this->getCount()>0)
$this->clear();
if($data instanceof CMap)
$data=$data->_d;
foreach($data as $key=>$value)
$this->add($key,$value);
}
else if($data!==null)
throw new CException(Yii::t('yii','Map data must be an array or an object implementing Traversable.'));
}
灏嗚凯浠e櫒涓殑鏁版嵁澶嶅埗鍒癿ap銆 娉ㄦ剰锛屽垪琛ㄤ腑宸茬粡瀛樺湪鐨勬暟鎹細琚鍏堝垹闄ゃ
public integer count()
| ||
{return} | integer | 杩斿洖map涓殑椤圭洰鏁般 |
杩斿洖map涓殑椤圭洰鏁般 姝ゆ柟娉曚负鎺ュ彛Countable寮哄埗瑕佹眰瀹炵幇銆
public integer getCount()
| ||
{return} | integer | 杩斿洖map涓殑椤圭洰鏁 |
杩斿洖map涓殑椤圭洰鏁般
public CMapIterator getIterator()
| ||
{return} | CMapIterator | 杩斿洖閬嶅巻鍒楄〃涓厓绱犵殑杩唬鍣ㄣ |
public function getIterator()
{
return new CMapIterator($this->_d);
}
杩斿洖閬嶅巻杩欎釜鍒楄〃鐨勯」鐩殑杩唬鍣ㄣ 姝ゆ柟娉曚负鎺ュ彛IteratorAggregate寮哄埗瑕佹眰瀹炵幇銆
public array getKeys()
| ||
{return} | array | 杩斿洖閿悕鍒楄〃 |
public function getKeys()
{
return array_keys($this->_d);
}
public boolean getReadOnly()
| ||
{return} | boolean | 杩斿洖鍊艰鏄庤繖涓垪琛ㄦ槸鍚︿负鍙銆傞粯璁や负false銆 |
public mixed itemAt(mixed $key)
| ||
$key | mixed | 椤圭洰鐨勭储寮曞 |
{return} | mixed | 杩斿洖鎸囧畾浣嶇疆鐨勫厓绱狅紝濡傛灉杩欎釜浣嶇疆娌℃湁鍏冪礌瀛樺湪锛屽垯杩斿洖null銆 |
public function itemAt($key)
{
if(isset($this->_d[$key]))
return $this->_d[$key];
else
return null;
}
杩斿洖鎸囧畾浣嶇疆鐨勯」鐩 銆杩欎釜鏂规硶璺offsetGet鏄畬鍏ㄤ竴鏍风殑銆
public static array mergeArray(array $a, array $b)
| ||
$a | array | 瑕佽鍚堝苟鐨勬暟缁 |
$b | array | 瑕佽繘琛屽悎骞剁殑鏁扮粍銆備綘鍙互閫氳繃绗笁锛屽洓銆傘傘 鎸囧畾棰濆銆 |
{return} | array | 杩斿洖鍚堝苟鍚庣殑鏁扮粍锛堝師鏁扮粍涓嶄細鍙戠敓鏀瑰彉銆傦級 |
public static function mergeArray($a,$b)
{
$args=func_get_args();
$res=array_shift($args);
while(!empty($args))
{
$next=array_shift($args);
foreach($next as $k => $v)
{
if(is_integer($k))
isset($res[$k]) ? $res[]=$v : $res[$k]=$v;
else if(is_array($v) && isset($res[$k]) && is_array($res[$k]))
$res[$k]=self::mergeArray($res[$k],$v);
else
$res[$k]=$v;
}
}
return $res;
}
閫掑綊鍚堝苟涓や釜鎴栧涓暟缁勩 濡傛灉姣忎釜鏁扮粍閮藉寘鍚潃鐩稿悓绱㈠紩鐨勫厓绱狅紝鍒欏悗闈㈢殑 浼氳鐩栧墠闈㈢殑锛堣繖涓窡array_merge_recursive鏈夊尯鍒級銆 濡傛灉涓や釜鏁扮粍鐨勫厓绱犳槸鏁扮粍锛屼笖鏄浉鍚岀殑閿悕锛屽垯浼 杩涜閫掑綊鍚堝苟銆 瀵逛簬浠ユ暟瀛椾綔涓洪敭鍚嶇殑鍏冪礌锛屽悗闈㈡暟缁勭殑鍏冪礌浼氭坊鍔犲埌 鍓嶉潰鐨勬暟缁勯噷闈€
鍙傝
public void mergeWith(mixed $data, boolean $recursive=true)
| ||
$data | mixed | 瑕佸悎骞剁殑鏁版嵁, 鍙兘鏄暟缁勬垨鑰呯户鎵夸簬Traversable鐨勫璞 |
$recursive | boolean | 鏍囪瘑鏄惁杩涜閫掑綊鍚堝苟銆 |
public function mergeWith($data,$recursive=true)
{
if(is_array($data) || $data instanceof Traversable)
{
if($data instanceof CMap)
$data=$data->_d;
if($recursive)
{
if($data instanceof Traversable)
{
$d=array();
foreach($data as $key=>$value)
$d[$key]=$value;
$this->_d=self::mergeArray($this->_d,$d);
}
else
$this->_d=self::mergeArray($this->_d,$data);
}
else
{
foreach($data as $key=>$value)
$this->add($key,$value);
}
}
else if($data!==null)
throw new CException(Yii::t('yii','Map data must be an array or an object implementing Traversable.'));
}
灏嗚凯浠e櫒鐨勬暟鎹暣鍚堝埌map銆
濡傛灉婧愪腑鐨勭殑閿肩浉鍚岋紝map涓殑鐜版湁鍏冪礌灏嗚瑕嗙洊銆
濡傛灉鍚堝苟鏄掑綊鐨勶紝鎵ц涓嬮潰鐨勭畻娉曪細
- map鐨勬暟鎹繚瀛樹负$a锛屾簮鏁版嵁淇濆瓨涓$b锛
- 濡傛灉$a鍜$b閮藉瓨鍦ㄧ浉鍚岀殑瀛楃涓查敭绱㈠紩鐨勬暟缁勶紝鏁扮粍灏嗕娇鐢ㄨ繖绉嶇畻娉曡繘琛屽悎骞讹紱
- 浠讳綍鍦$b涓互鏁存暟浣滀负绱㈠紩鐨勫厓绱狅紝閮戒細鐩存帴鍦$a涓噸鏂拌繘琛岀储寮曪紝鐒跺悗鍔犲埌$a閲岄潰锛
- 浠讳綍鍦$b涓互瀛楃涓蹭綔涓虹储寮曠殑鍏冪礌锛屼細瑕嗙洊閭d簺鍦$a涓浉鍚岀储寮曞肩殑鍏冪礌锛
public boolean offsetExists(mixed $offset)
| ||
$offset | mixed | 瑕佹鏌ョ殑浣嶇疆 |
{return} | boolean |
public function offsetExists($offset)
{
return $this->contains($offset);
}
杩斿洖鍊艰鏄庢寚瀹氫綅缃槸鍚﹀瓨鍦ㄥ厓绱犮 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public mixed offsetGet(integer $offset)
| ||
$offset | integer | 瑕佹鏌ョ殑浣嶇疆銆 |
{return} | mixed | 杩斿洖杩欎釜浣嶇疆鐨勫厓绱狅紝濡傛灉璇ヤ綅缃笉瀛樺湪鍏冪礌锛屽垯杩斿洖null銆 |
public function offsetGet($offset)
{
return $this->itemAt($offset);
}
杩斿洖鍊兼寚瀹氫綅缃殑鍏冪礌銆 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public void offsetSet(integer $offset, mixed $item)
| ||
$offset | integer | 瑕佽缃厓绱犵殑浣嶇疆 |
$item | mixed | 鍏冪礌瀵硅薄 |
public function offsetSet($offset,$item)
{
$this->add($offset,$item);
}
璁剧疆鎸囧畾浣嶇疆鐨勫厓绱犮 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public void offsetUnset(mixed $offset)
| ||
$offset | mixed | 瑕佸垹闄ょ殑鍏冪礌鐨勪綅缃 |
public function offsetUnset($offset)
{
$this->remove($offset);
}
鍒犻櫎鎸囧畾浣嶇疆鐨勫厓绱犮 姝ゆ柟娉曚负鎺ュ彛ArrayAccess寮哄埗瑕佹眰瀹炵幇銆
public mixed remove(mixed $key)
| ||
$key | mixed | 瑕佸垹闄ょ殑椤圭洰鐨勯敭鍚 |
{return} | mixed | 杩斿洖宸茬粡鍒犻櫎鐨勫硷紝濡傛灉娌℃湁杩欐牱鐨勯」鐩瓨鍦紝鍒欒繑鍥瀗ull銆 |
public function remove($key)
{
if(!$this->_r)
{
if(isset($this->_d[$key]))
{
$value=$this->_d[$key];
unset($this->_d[$key]);
return $value;
}
else
{
// it is possible the value is null, which is not detected by isset
unset($this->_d[$key]);
return null;
}
}
else
throw new CException(Yii::t('yii','The map is read only.'));
}
鏍规嵁閿悕浠巑ap涓垹闄や竴涓」鐩
protected void setReadOnly(boolean $value)
| ||
$value | boolean | 璁剧疆杩欎釜鍒楄〃鏄惁涓哄彧璇 |
protected function setReadOnly($value)
{
$this->_r=$value;
}
public array toArray()
| ||
{return} | array | 鏁扮粍涓殑椤圭洰鍒楄〃 |