SplObjectStorage::key()函数是PHP中的一个内置函数,用于获取当前指向的迭代器的索引。
用法:
int SplObjectStorage::key()
参数:该函数不接受任何参数。
返回值:此函数返回迭代器当前指向的索引。
以下程序说明了PHP中的SplObjectStorage::key()函数:
示例1:
<?php
// Create an empty SplObjectStorage
$str = new SplObjectStorage();
$obj = new StdClass;
$str->attach($obj, "d1");
$str->rewind();
// Get current index
$index = $str->key();
// Print Result
var_dump($index);
?>
输出:
int(0)
示例2:
<?php
// Create an Empty SplObjectStorage
$str = new SplObjectStorage();
$obj1 = new StdClass;
$obj2 = new StdClass;
$obj3 = new StdClass;
$obj4 = new StdClass;
$str->attach($obj1, "GeksforGeeks");
$str->attach($obj2, "GFG");
$str->attach($obj3);
$str->attach($obj4, "DSA");
$str->rewind();
// Iterate and print data on each index
while($str->valid()) {
// Get index
$index = $str->key();
$object = $str->current();
$data = $str->getInfo();
var_dump($index, $data);
$str->next();
}
?>
输出:
int(0) string(12) "GeksforGeeks" int(1) string(3) "GFG" int(2) NULL int(3) string(3) "DSA"
参考: https://www.php.net/manual/en/splobjectstorage.key.php
相关用法
- PHP SplObjectStorage contains()用法及代码示例
- PHP SplObjectStorage next()用法及代码示例
- PHP SplObjectStorage offsetExists()用法及代码示例
- PHP SplObjectStorage count()用法及代码示例
- PHP SplObjectStorage offsetSet()用法及代码示例
- PHP SplObjectStorage detach()用法及代码示例
- PHP SplObjectStorage getinfo()用法及代码示例
- PHP SplObjectStorage current()用法及代码示例
- PHP SplObjectStorage serialize()用法及代码示例
- PHP SplObjectStorage setInfo()用法及代码示例
- PHP SplObjectStorage unserialize()用法及代码示例
- PHP SplObjectStorage rewind()用法及代码示例
- PHP SplObjectStorage offsetGet()用法及代码示例
- PHP SplObjectStorage offsetUnset()用法及代码示例
- PHP SplObjectStorage removeAll()用法及代码示例
注:本文由纯净天空筛选整理自R_Raj大神的英文原创作品 PHP | SplObjectStorage key() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。