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


PHP AppendIterator getIteratorIndex()用法及代碼示例


AppendIterator::getIteratorIndex()函數是PHP中的內置函數,用於獲取當前內部迭代器的索引。

用法:

int AppendIterator::getIteratorIndex( void )

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


返回值:此函數返回一個整數值,該值是當前內部迭代器的從零開始的索引。

以下示例程序旨在說明PHP中的AppendIterator::getIteratorIndex()函數:

示例1:

<?php 
  
// Declare an ArrayIterator 
$arr1 = new ArrayIterator(array("Geeks", "for", "Geeks")); 
$arr2 = new ArrayIterator(array("Computer", "Science", "Portal")); 
  
// Create a new AppendIterator 
$itr = new AppendIterator; 
$itr->append($arr1); 
$itr->append($arr2); 
  
// Display the elements 
foreach ($itr as $key => $val) { 
    echo "Iterator Index: " . $itr->getIteratorIndex() 
        . "  Key : " . $key . "  Value: " . $val . "\n";  
} 
  
?>
輸出:
Iterator Index: 0  Key : 0  Value: Geeks
Iterator Index: 0  Key : 1  Value: for
Iterator Index: 0  Key : 2  Value: Geeks
Iterator Index: 1  Key : 0  Value: Computer
Iterator Index: 1  Key : 1  Value: Science
Iterator Index: 1  Key : 2  Value: Portal

示例2:

<?php 
  
// Declare an ArrayIterator 
$arr1 = new ArrayIterator( 
    array( 
        "a" => "Geeks", 
        "b" => "for", 
        "c" => "Geeks"
    ) 
); 
  
$arr2 = new ArrayIterator( 
    array( 
        "x" => "Computer", 
        "y" => "Science", 
        "z" => "Portal"
    ) 
); 
  
// Create a new AppendIterator 
$itr = new AppendIterator; 
$itr->append($arr1); 
$itr->append($arr2); 
  
// Display the elements 
foreach ($itr as $key => $val) { 
    echo "Iterator Index: " . $itr->getIteratorIndex() 
        . "  Key : " . $key . "  Value: " . $val . "\n";  
} 
  
?>
輸出:
Iterator Index: 0  Key : a  Value: Geeks
Iterator Index: 0  Key : b  Value: for
Iterator Index: 0  Key : c  Value: Geeks
Iterator Index: 1  Key : x  Value: Computer
Iterator Index: 1  Key : y  Value: Science
Iterator Index: 1  Key : z  Value: Portal

參考: https://www.php.net/manual/en/appenditerator.getiteratorindex.php



相關用法


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