当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP SplObjectStorage next()用法及代码示例


SplObjectStorage::next()函数是PHP中的内置函数,用于移至下一个存储项。

用法:

void SplObjectStorage::next()

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


返回值:该函数不返回任何值。

以下程序说明了PHP中的SplObjectStorage::next()函数:

示例1:

<?php 
  
// Create an empty SplObjectStorage 
$str = new SplObjectStorage(); 
  
$obj = new StdClass; 
$obj2 = new StdClass; 
  
// Use attach() function to add object 
$str->attach($obj, "GFG"); 
$str->attach($obj2, "Geeks"); 
  
$str->rewind(); 
  
// Get the current data  
var_dump($str->getInfo()); 
  
// Move on to next object 
$str->next(); 
  
// Print result of next entry 
var_dump($str->getInfo()); 
?>
输出:
string(3) "GFG"
string(5) "Geeks"

示例2:

<?php 
  
// Create an Empty SplObjectStorage 
$str = new SplObjectStorage(); 
   
$obj1 = new StdClass; 
$obj2 = new StdClass; 
$obj3 = new StdClass; 
$obj4 = new StdClass; 
   
// Use attach() function to add object 
$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); 
      
    // Moving each time next entry 
    $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.next.php



相关用法


注:本文由纯净天空筛选整理自R_Raj大神的英文原创作品 PHP | SplObjectStorage next() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。