PHP | CachingIterator key()函数
CachingIterator::key()函数是PHP中的一个内置函数,用于返回当前元素的键。
句法:
scalar CachingIterator::key( void )
参数:此函数不接受任何参数。
返回值:该函数返回当前元素的键值。
下面的程序说明了PHP中的 CachingIterator::key()函数:
方案一:
key() . "\n";
}
?>
输出:
0
1
2
3
4
方案二:
"Geeks",
"b" => "for",
"c" => "Geeks",
"d" => "Computer",
"e" => "Science",
"f" => "Portal"
);
// Create a new CachingIterator
$cachIt = new CachingIterator(
new ArrayIterator($arr),
CachingIterator::FULL_CACHE
);
foreach($cachIt as $element) {
echo "key: " . $cachIt->key() . " Value: "
. $cachIt->current() . "\n";
}
?>
输出:
key: a Value: Geeks
key: b Value: for
key: c Value: Geeks
key: d Value: Computer
key: e Value: Science
key: f Value: Portal
参考: https://www. PHP.net/manual/en/cachingiterator.key。 PHP