📜  PHP | SplObjectStorage key()函数

📅  最后修改于: 2022-05-13 01:56:21.767000             🧑  作者: Mango

PHP | SplObjectStorage key()函数

SplObjectStorage::key()函数是PHP中的一个内置函数,用于获取当前指向的迭代器的索引。

句法:

int SplObjectStorage::key()

参数:此函数不接受任何参数。

返回值:此函数返回迭代器当前指向的索引。

下面的程序说明了PHP中的SplObjectStorage::key()函数:

方案一:

attach($obj, "d1");
  
$str->rewind();
  
// Get current index 
$index  = $str->key();
  
// Print Result
var_dump($index);
?>
输出:
int(0)

方案二:

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