當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP SplObjectStorage rewind()用法及代碼示例


SplObjectStorage::rewind()函數是PHP中的內置函數,用於將迭代器倒回第一個存儲元素。

用法:

void SplObjectStorage::rewind()

參數:該函數不接受任何參數。


返回值:該函數不返回任何值。

以下程序說明了PHP中的SplObjectStorage::rewind()函數:

示例1:

<?php 
  
// Create an empty SplObjectStorage 
$str = new SplObjectStorage(); 
  
$obj = new StdClass; 
$obj2 = new StdClass; 
$obj3 = new StdClass; 
  
$str->attach($obj, "GFG"); 
$str->attach($obj2, "Geeks"); 
$str->attach($obj3, "FORK JAVA"); 
  
// Using rewind function 
$str->rewind(); 
  
// Get current data  
var_dump($str->getInfo()); 
  
// Move on to next object 
$str->next(); 
  
// Get current data  
var_dump($str->getInfo()); 
  
// Again using rewind function 
$str->rewind(); 
  
// Get current data  
var_dump($str->getInfo()); 
?>
輸出:
string(3) "GFG"
string(5) "Geeks"
string(3) "GFG"

示例2:

<?php 
  
$obj1 = new StdClass; 
$obj2 = new StdClass; 
$obj3 = new StdClass; 
  
$gfg = new SplObjectStorage(); 
$gfg[$obj1] = "GFG"; 
$gfg[$obj2] = "GeeksClasses"; 
$gfg[$obj3] = "SUDO"; 
  
// Using rewind function 
$gfg->rewind(); 
  
while($gfg->valid()) { 
    var_dump($gfg->getInfo()); 
      
    // Moving to next element 
    $gfg->next(); 
} 
?>
輸出:
string(3) "GFG"
string(12) "GeeksClasses"
string(4) "SUDO"

參考: https://www.php.net/manual/en/splobjectstorage.rewind.php



相關用法


注:本文由純淨天空篩選整理自R_Raj大神的英文原創作品 PHP | SplObjectStorage rewind() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。