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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。