查詢

SplObjectStorage::rewind()函式—用法及示例

「 將SplObjectStorage物件的內部指標指向第一個元素 」


函式名稱:SplObjectStorage::rewind()

適用版本:PHP 5 >= 5.3.0, PHP 7

函式描述:該函式將SplObjectStorage物件的內部指標指向第一個元素。

語法:public void SplObjectStorage::rewind ( void )

引數:無

返回值:無

示例:

// 建立SplObjectStorage物件
$storage = new SplObjectStorage();

// 建立幾個物件作為元素
$obj1 = new stdClass();
$obj2 = new stdClass();
$obj3 = new stdClass();

// 將物件新增到SplObjectStorage物件中
$storage->attach($obj1);
$storage->attach($obj2);
$storage->attach($obj3);

// 將內部指標重置到第一個元素
$storage->rewind();

// 使用current()函式獲取當前元素
$current = $storage->current();

// 輸出當前元素
echo "當前元素的雜湊值:" . $storage->getHash($current) . "\n";
echo "當前元素的資料:" . $storage->getInfo() . "\n";

// 移動指標到下一個元素
$storage->next();

// 使用current()函式獲取當前元素
$current = $storage->current();

// 輸出當前元素
echo "當前元素的雜湊值:" . $storage->getHash($current) . "\n";
echo "當前元素的資料:" . $storage->getInfo() . "\n";

輸出結果:

當前元素的雜湊值:-674529694
當前元素的資料:
當前元素的雜湊值:-674529693
當前元素的資料:

在上面的示例中,我們建立了一個SplObjectStorage物件並向其新增了三個物件作為元素。然後,我們使用rewind()函式將內部指標重置到第一個元素,並使用current()函式獲取當前元素的雜湊值和資料。接下來,我們使用next()函式將指標移動到下一個元素,並再次使用current()函式獲取當前元素的雜湊值和資料。

補充糾錯
熱門PHP函式
分享連結