AppendIterator::key()函數是PHP中的內置函數,用於返回當前 key 。
用法:
scalar AppendIterator::key( void )
參數:該函數不接受任何參數。
返回值:如果有效,則此函數返回當前 key ,否則返回NULL。
以下示例程序旨在說明PHP中的AppendIterator::key()函數:
示例1:
<?php
// Declare an ArrayIterator
$arr1 = new ArrayIterator(array('G', 'e', 'e', 'k', 's'));
// Create a new AppendIterator
$itr = new AppendIterator;
// Append the ArrayIterator element
$itr->append($arr1);
// Display the result
while ($itr->valid()) {
echo "ArrayIterator Key: " . $itr->key() .
" ArrayIterator Value: " . $itr->current() . "\n";
$itr->next();
}
?>
輸出:
ArrayIterator Key: 0 ArrayIterator Value: G ArrayIterator Key: 1 ArrayIterator Value: e ArrayIterator Key: 2 ArrayIterator Value: e ArrayIterator Key: 3 ArrayIterator Value: k ArrayIterator Key: 4 ArrayIterator Value: s
示例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);
$itr->rewind();
// Display the result
while ($itr->valid()) {
echo "ArrayIterator Key: " . $itr->key() .
" ArrayIterator Value: " . $itr->current() . "\n";
$itr->next();
}
?>
輸出:
ArrayIterator Key: a ArrayIterator Value: Geeks ArrayIterator Key: b ArrayIterator Value: for ArrayIterator Key: c ArrayIterator Value: Geeks ArrayIterator Key: x ArrayIterator Value: Computer ArrayIterator Key: y ArrayIterator Value: Science ArrayIterator Key: z ArrayIterator Value: Portal
參考: https://www.php.net/manual/en/appenditerator.key.php
相關用法
- PHP AppendIterator next()用法及代碼示例
- PHP AppendIterator rewind()用法及代碼示例
- PHP AppendIterator valid()用法及代碼示例
- PHP AppendIterator append()用法及代碼示例
- PHP AppendIterator __construct()用法及代碼示例
- PHP AppendIterator current()用法及代碼示例
- PHP AppendIterator getIteratorIndex()用法及代碼示例
- d3.js d3.lab()用法及代碼示例
- PHP dir()用法及代碼示例
- p5.js int()用法及代碼示例
- d3.js d3.rgb()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
注:本文由純淨天空篩選整理自jit_t大神的英文原創作品 PHP | AppendIterator key() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。