PHP | AppendIterator key()函数
AppendIterator::key()函数是PHP中的一个内置函数,用于返回当前键。
句法:
scalar AppendIterator::key( void )
参数:此函数不接受任何参数。
返回值:如果当前键有效,则此函数返回当前键,否则返回 NULL。
下面的程序说明了PHP中的 AppendIterator::key()函数:
方案一:
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
方案二:
"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